FileNotFoundError: [Errno 2] No such file or directory [Fix]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
7 min

banner

# FileNotFoundError: [Errno 2] No such file or directory

The most common causes of the "FileNotFoundError: [Errno 2] No such file or directory" error are:

  1. Trying to open a file that doesn't exist in the specified location.
  2. Misspelling the name of the file or a path component.
  3. Forgetting to specify the extension of the file. Note that Windows doesn't display file extensions.

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.

filenotfounderror no such file or directory

Here is an example of how the error occurs.

main.py
# โ›”๏ธ 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.

This is a relative path, so Python looks for example-file.txt in the same directory as the Python script (main.py) in the example.
shell
# ๐Ÿ‘‡๏ธ 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.

# Move the file to the same directory as your Python script

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.

main.py
import os current_directory = os.getcwd() # ๐Ÿ‘‡๏ธ /home/borislav/Desktop/bobbyhadz_python print(current_directory)

print current working 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.

main.py
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.

example.txt
bobby hadz com

move file next to your python script

# Specifying an absolute path to the file

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).

main.py
# ๐Ÿ‘‡๏ธ 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.

main.py
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)

using an absolute path to a file

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:

  1. Right-clicking on the file.
  2. Click on "Properties".

find absolute path to file

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.

find path to file in finder

Make sure to add the filename and the extension at the end of the absolute path.

# Make sure to use a raw string if the path contains backslashes

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.

main.py
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.

# Using a relative path to open the file

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.

shell
my_project/ main.py nested_folder/ example.txt

You can open the example.txt file from your main.py script as follows.

main.py
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.

shell
Desktop/ my_project/ main.py example.txt

To open the example.txt file, navigate one directory up.

main.py
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.

# Checking if the specified file exists before opening it

You can use the os module to print the current working directory and its contents.

main.py
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.

If you don't see the file you are trying to open in the list, you shouldn't try to open the file with a relative path (e.g. 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.

# Creating a file with the specified name if it doesn't exist

You can use this approach to create a file with the specified name if it doesn't already exist.

main.py
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 you have specified the correct filename

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.

# Changing to the directory that contains the file

An alternative solution is to change to the directory that contains the file you are trying to open using the os.chdir() method.

main.py
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.

# Things to node when debugging

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.

main.py
my_path = r'C:\Users\Alice\Desktop\my-file.txt' print(my_path) # ๐Ÿ‘‰๏ธ C:\Users\Alice\Desktop\my-file.txt
Strings that are prefixed with 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.

main.py
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.

main.py
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.

shell
# ๐Ÿ‘‡๏ธ 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.

shell
# ๐Ÿ‘‡๏ธ 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.

shell
../example.txt

The relative path assumes that there is an example.txt file one directory up from your Python script.

shell
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.

main.py
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.

# Conclusion

To solve the "FileNotFoundError: [Errno 2] No such file or directory" error, make sure:

  1. The file you are trying to open exists at the specified location.
  2. You haven't misspelled the name of the file or the path.
  3. You have included the extension of the file in the path. Note that Windows doesn't display file extensions.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev