Solve Python: Can't Open File 'setup.py': [Errno 2] No Such File or Directory Error

Solve Python: Can't Open File 'setup.py': [Errno 2] No Such File or Directory Error

We will learn, with this explanation, how to solve the FileNotFoundError or No such file or directory error in Python.

Solve the Python: can't open file 'setup.py': [Errno 2] No such file or directory Error

We have a simple Python script to open a file, read the contents, and display it, but we are getting this error (FileNotFoundError). So, we will show you how to solve this error and why this occurs.

Example Code:

Example=open('test.txt','r')
Example=Example.read()
print(Example)

Output:

PS C:\WINDOWS\System32\WindowsPowerShell\v1.0> python -u "f:\example\python can't
open file 'setup.py' [errno 2] no such file or directory\example.py"
Traceback (most recent call last):
  File "f:\example\python can't open file 'setup.py' [errno 2] no such file or directory\example.py", line 1, in <module>
    Example=open('test.txt','r')
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'

The basic reason for this error is that our file is not present in the same location as our Python script. One simple way to solve it is to copy this test.txt file and paste this file where the Python script is located so that this script will work.

There is another way to solve this error without a copy of this test.txt in the same folder, but we will need to give the absolute path or complete path where we pass the file name inside the Python script.

Now, we need to go to the folder where our test.txt file is located and click Navigation. And we will get the location of this folder, copy it, go back to the Python script, and then paste it where we pass the file name.

Example=open(r"C:\Users\Dell\Desktop\test\test.txt","r")
Example=Example.read()
print(Example)

We are using r before the double quotation marks to avoid the escape character, and if we run this Python script, we will not get any error. Instead of getting errors, we get the output.

So this is how we solve this. And if you are still getting this error, you have made silly mistakes in naming the file, so you must ensure your file name is correct.

This approach will work anywhere. For example, if you run the setup.py file, you have to specify the complete path where this file is located or install any Python package so that you have to access the complete path to the installation.

Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn

Related Article - Python Error

  • Python PermissionError: [WinError 5] Access Is Denied
  • Python TypeError: 'DataFrame' Object Is Not Callable
  • Python TypeError: Can't Convert 'List' Object to STR
  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10