The error FileNotFoundError: [Errno 2] No such file or directory occurs when you try to access a file that does not exist in the given path. The error occurs for various reasons. Let us look at each one of them.
The error occurs due to various reasons, let us look at each one of them.
Table of Contents
What causes FileNotFoundError: [Errno 2] No such file or directory?
When the system can’t find the file in the given path, this error is shown. The system might not find the file for any of the following reasons:
- You’ve specified the wrong path for the file.
- You’ve specified a relative path, and the file is not in the current working directory.
- You’ve mistakenly typed the wrong spelling or missed the file extensions.
Now, that we’ve understood what causes the issue, let’s check how to fix this error in each of the cases.
Fix 1: Check if the file you’re referring to is present in the path.
Sometimes the file you are referring to, is not present in the specified path. Double-check the path. If you use a relative path, you must know that the system searches for the file in the current working directory.
Problem :
Let’s say the file empty_obj.json is present in the Downloads folder, and we’re using the relative path, we see an error.
with open("empty_obj.json") as f: content=json.loads(f.read()) print(content)
Output:
Traceback (most recent call last): File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\json__learning.py", line 6, in <module> with open("empty_obj.json") as f: FileNotFoundError: [Errno 2] No such file or directory: 'empty_obj.json'
Explanation:
We are running the script json_learning.py, which is in the Solutions directory. However, there is no file named empty_obj.json in this directory. Thus, we see the FileNotFoundError.
Solution 1: Use absolute path
with open("C:/Users/paian/Downloads/empty_obj.json") as f: content=json.loads(f.read()) print(content)
Output:
{'data': [{'type': 'articles', 'id': '1', 'attributes': {'title': 'JSON:', 'body': 'The shortest article. Ever.', 'created': '2015-05-22T14:56:29.000Z', 'updated': '2015-05-22T14:56:28.000Z'}, 'relationships': {'author': {'data': {'id': '42', 'type': 'people'}}}}], 'included': [{'type': 'people', 'id': '42', 'attributes': {'name': 'John', 'age': 80, 'gender': 'male'}}]}
Solution 2:
If feasible, copy-paste the file you’re trying to access into the current working directory.
Are you seeing the SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape?
Fix 2: Check the spelling of your filename
If you have misspelled the filename, FileNotFoundError is thrown.
Problem :
Let’s say, we have a file named “empty_obj.json“, by mistake, we misspell it as “empy_obj.json“, and run the program, an error is seen.
with open("C:/Users/paian/Downloads/empy_obj.json") as f: content=json.loads(f.read()) print(content)
Output:
Traceback (most recent call last): File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\json__learning.py", line 6, in <module> with open("C:/Users/paian/Downloads/empy_obj.json") as f: FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/paian/Downloads/empy_obj.json'
Solution:
The error can be fixed by specifying the right spelling.
with open("C:/Users/paian/Downloads/empty_obj.json") as f: content=json.loads(f.read()) print(content)
Output:
{'data': [{'type': 'articles', 'id': '1', 'attributes': {'title': 'JSON:', 'body': 'The shortest article. Ever.', 'created': '2015-05-22T14:56:29.000Z', 'updated': '2015-05-22T14:56:28.000Z'}, 'relationships': {'author': {'data': {'id': '42', 'type': 'people'}}}}], 'included': [{'type': 'people', 'id': '42', 'attributes': {'name': 'John', 'age': 80, 'gender': 'male'}}]}
Fix 3: Check if you’re missing the file extension
If you haven’t specified the file extension in your path, you will see a FileNotFoundError.
Problem:
There is a file empty_obj.json in the Downloads folder. If we forget to specify the file extension in the path, this error occurs.
with open("C:/Users/paian/Downloads/empty_obj") as f: content=json.loads(f.read()) print(content)
Output:
Traceback (most recent call last): File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\json__learning.py", line 6, in <module> with open("C:/Users/paian/Downloads/empty_obj") as f: FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/paian/Downloads/empty_obj'
Solution:
To fix this error, specify the right extension as shown below.
with open("C:/Users/paian/Downloads/empty_obj.json") as f: content=json.loads(f.read()) print(content)
Output:
{'data': [{'type': 'articles', 'id': '1', 'attributes': {'title': 'JSON:API paints', 'body': 'The shortest article. Ever.', 'created': '2015-05-22T14:56:29.000Z', 'updated': '2015-05-22T14:56:28.000Z'}, 'relationships': {'author': {'data': {'id': '42', 'type': 'people'}}}}], 'included': [{'type': 'people', 'id': '42', 'attributes': {'name': 'John', 'age': 80, 'gender': 'male'}}]}
Conclusion
n this article, we’ve discussed possible causes of the FileNotFoundError: [Errno 2] No such file or directory. We’ve also suggested possible fixes to overcome this issue.
Thanks for reading.
Also check, TypeError: unhasable type:’dict’
2 thoughts on “[SOLVED] FileNotFoundError: [Errno 2] No such file or directory”