Last updated: Apr 8, 2024
Reading timeยท7 min
The most common causes of the "FileNotFoundError: [Errno 2] No such file or directory" error are:
To solve the error, move the file to the directory where the Python script is located if using a local path, or use an absolute path.
Here is an example of how the error occurs.
# โ๏ธ FileNotFoundError: [Errno 2] No such file or directory: 'example-file.txt' with open('example-file.txt', 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)
We tried to open a file called example-file.txt
and it wasn't found in the
specified directory.
example-file.txt
in the same directory as the Python script (main.py
) in the example.# ๐๏ธ Relative path (the file has to be in the same directory) example.txt # ๐๏ธ Absolute path (Windows) 'C:\Users\Alice\Desktop\my-file.txt' # ๐๏ธ Absolute path (macOS or Linux) '/home/alice/Desktop/my-file.txt'
An absolute path is a complete path that points to the file (including the extension).
Note that Windows doesn't display file extensions.
One way to solve the error is to move the file to the same directory as the Python script.
You can use the following 3 lines to print the current working directory (of your Python script) if you don't know it.
import os current_directory = os.getcwd() # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python print(current_directory)
For example, if you have a Python script called main.py
, you would move the
example.txt
file right next to the main.py
file.
with open('example.txt', 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)
The code sample assumes that you have an example.txt
file in the same
directory as the main.py
file.
bobby hadz com
Alternatively, you can specify an absolute path to the file in the call to the open() function.
An absolute file that points to the file might look something like the following (depending on your operating system).
# ๐๏ธ On macOS or Linux my_str = r'/home/alice/Desktop/my-file.txt' # ๐๏ธ On Windows my_str_2 = r'C:\Users\Alice\Desktop\my-file.txt'
Here is a complete example that uses an absolute path to open a file.
absolute_path = r'/home/borislav/Desktop/bobbyhadz_python/example.txt' with open(absolute_path, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)
Make sure to specify the entire path to the file, including the extension.
On most operating systems, you can find the absolute path to a file by:
The path that is shown will likely not include the name of the file, but will point to the directory that contains the file.
Make sure to specify the name of the file and the extension at the end.
You can also open the file in Finder and look at the path.
If your path contains backslashes, e.g. 'C:\Users\Alice\Desktop\my-file.txt'
,
prefix the string with r
to mark it as a
raw string.
my_path = r'C:\Users\Alice\Desktop\my-file.txt'
Backslashes are used as escape characters (e.g. \n
or \t
) in Python, so by
prefixing the string with an r
, we treat backslashes as literal characters.
You can also use a relative path, even if the file is not located in the same directory as your Python script.
Assuming that you have the following folder structure.
my_project/ main.py nested_folder/ example.txt
You can open the example.txt
file from your main.py
script as follows.
with open(r'nested_dir/example.txt', 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)
We first navigate to the nested_dir
directory and then open the file.
If your file is located one or more directories up you have to prefix the path
with ../
.
For example, assume that you have the following folder structure.
Desktop/ my_project/ main.py example.txt
To open the example.txt
file, navigate one directory up.
with open(r'../example.txt', 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)
The same syntax can be used to open a file that is located two directories up,
e.g. ../../example.txt
.
You can use the os
module to print the current working directory and its
contents.
import os current_directory = os.getcwd() # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python print(current_directory) contents = os.listdir(current_directory) print(contents) # ๐๏ธ ['main.py', 'example.py', ...] # ๐๏ธ Check if the file is in current directory print('example-file.txt' in contents) # ๐๏ธ False
The os.getcwd() method returns a string that represents the current working directory.
The os.listdir() method returns a list that contains the names of the entries in the directory of the specified path.
The code sample prints the contents of the current working directory.
example.txt
).Instead, you should use an absolute path (e.g.
r'C:\Users\Alice\Desktop\my-file.txt'
).
We used the in
operator to check if a file with the specified name exists.
You can use this approach to create a file with the specified name if it doesn't already exist.
import os current_directory = os.getcwd() contents = os.listdir(current_directory) filename = 'example.txt' if filename in contents: with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines) else: with open(filename, 'w', encoding='utf-8') as my_file: my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n')
We used the in
operator to check if the file exists.
If it doesn't exist, the else
block runs where we create a file with the same
name, adding 3 lines to it.
If you need to create the file name using variables, check out the following article.
Make sure a file with the specified name exists and you haven't misspelled the file's name.
The name of the file shouldn't contain any special characters, e.g.
backslashes \
, forward slashes /
or spaces.
I've also written a guide on taking a file path from user input.
An alternative solution is to change to the directory that contains the file you
are trying to open using the os.chdir()
method.
import os dir_containing_file = r'/home/borislav/Desktop/bobbyhadz_python' # ๐๏ธ Change to the directory containing the file os.chdir(dir_containing_file) file_name = 'example.txt' with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)
The os.chdir() method allows us to change the current working directory to the specified path.
Notice that I passed an absolute path to the method.
The example above assumes that there is an example.txt
file in the
/home/borislav/Desktop/bobbyhadz_python
directory.
Once we use the os.chdir()
method to change to the directory that contains the
file, we can pass the filename directory to the open()
function.
Make sure to specify the extension of the file. Note that Windows doesn't display file extensions.
If your path contains backslashes, make sure to prefix it with an r
to mark it
as a raw string.
my_path = r'C:\Users\Alice\Desktop\my-file.txt' print(my_path) # ๐๏ธ C:\Users\Alice\Desktop\my-file.txt
r
are called raw strings and treat backslashes as literal characters.If you don't mark the string as raw, you have to escape every backslash with another backslash to treat them as literal characters.
my_path = 'C:\\Users\\Alice\\Desktop\\my-file.txt' print(my_path) # ๐๏ธ C:\Users\Alice\Desktop\my-file.txt
You can also use forward slashes in a path instead of backslashes.
my_path = 'C:/Users/Alice/Desktop/my-file.txt' print(my_path) # ๐๏ธ C:/Users/Alice/Desktop/my-file.txt
Python supports the use of forward slashes in a Windows path. Notice that we don't have to escape forward slashes or mark the string as raw.
When calling the open()
function with an absolute path, make sure it starts
from your user's root directory.
# ๐๏ธ Absolute path (Windows) 'C:\Users\Alice\Desktop\my-file.txt' # ๐๏ธ Absolute path (macOS or Linux) '/home/alice/Desktop/my-file.txt'
If you call the open()
function with a relative path that only contains a file
name, make sure the file is located in the same directory as your Python script.
# ๐๏ธ Relative path example.txt
The relative path above assumes that there is an example.txt
file in the same
directory as your Python script (e.g. main.py
).
If you need to go one directory up, use a ../
prefix.
../example.txt
The relative path assumes that there is an example.txt
file one directory up
from your Python script.
example_folder/ example.txt # ๐๏ธ One directory up from main.py src/ main.py
You can use multiple ../
prefixes to go multiple directories up, e.g.
../../example.txt
.
If none of the suggestions helped, use the os.chdir()
method to change the
directory of your script before opening the file.
import os dir_containing_file = r'/home/borislav/Desktop/bobbyhadz_python' # ๐๏ธ Change to the directory containing the file os.chdir(dir_containing_file) file_name = 'example.txt' with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)
The code sample above assumes that there is a file called example.txt
in the
'/home/borislav/Desktop/bobbyhadz_python'
directory.
Once we change to the directory with the os.chdir()
method, we can pass the
filename directly to the open()
function.
To solve the "FileNotFoundError: [Errno 2] No such file or directory" error, make sure:
You can learn more about the related topics by checking out the following tutorials: