FileExistsError: [Errno 17] File exists in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
2 min

banner

# FileExistsError: [Errno 17] File exists in Python

The Python "FileExistsError: [Errno 17] File exists" occurs when we try to create a directory that already exists.

To solve the error, set the exist_ok keyword argument to True in the call to the os.makedirs() method, e.g. os.makedirs(dir_name, exist_ok=True).

fileexistserror errno 17 file exists

Here is an example of how the error occurs.

main.py
import os dir_name = 'my_dir' # ⛔️ FileExistsError: [Errno 17] File exists: 'my_dir' os.makedirs(dir_name)

directory already exists

The my_dir directory already exists, so a FileExistsError is raised.

# Set the exist_ok argument to True when calling makedirs()

One way to resolve the error is to set the exist_ok keyword argument to True.

main.py
import os dir_name = 'my_dir' os.makedirs(dir_name, exist_ok=True)

setting exist ok to true

Make sure to use the os.makedirs() method, and not os.makedir() as the latter doesn't take a exist_ok keyword argument.

If exist_ok is set to True, you won't get a FileExistsError if the target directory already exists.

The value for the exist_ok parameter is set to False by default.

# Using a try/except statement to handle the error

Alternatively, you can use a try/except statement to handle the FileExistsError.

main.py
import os dir_name = 'my_dir' try: os.makedirs(dir_name) except FileExistsError: print('The directory already exists')

using try except statement

We try to create the directory in the try block and if a FileExistsError is raised, the except block is run.

You can use the pass statement if you don't want to handle the error in any way.

main.py
import os dir_name = 'my_dir' try: os.makedirs(dir_name) except FileExistsError: pass

The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.

The os.makedirs() method is used for recursive directory creation.

It makes all the intermediate-level directories needed to contain the nested directory.

Setting the exist_ok keyword argument to True makes it so the method doesn't raise an error if a directory with the specified name already exists.

The exist_ok keyword argument is set to False by default.

# Checking if a path exists

If you need to check if a path exists, use the os.path.exists() method.

main.py
import os path = r'/home/borislav/Desktop/bobbyhadz_python/' # 👇️ True print(os.path.exists(os.path.dirname(path))) # 👇️ /home/borislav/Desktop/bobbyhadz_python print(os.path.dirname(path))

checking if path exists

The os.path.exists() method returns True if the provided path exists and False otherwise.

We also used the os.path.dirname() method to get the directory name of the path.

You can use an if statement to check if the directory of the given path exists or does not exist.

main.py
import os path = r'/home/borislav/Desktop/bobbyhadz_python/my_dir' if not os.path.exists(os.path.dirname(path)): print('The path does not exist') os.makedirs(os.path.dirname(path)) else: print('The path already exists')

The if statement checks if the path doesn't exist.

If the condition is met, we use the os.path.makedirs method to create the directory.

You can also wrap the if/else statements in a try/except statement.

main.py
import os path = r'/home/borislav/Desktop/bobbyhadz_python/my_dir' try: if not os.path.exists(os.path.dirname(path)): print('The path does not exist') os.makedirs(os.path.dirname(path)) else: print('The path already exists') except OSError: print('An OSError exception occurred')

If an OSError exception is raised at any point, it gets handled by the except block.

The OSError exception is a parent class to many other exceptions, such as FileExistsError and FileNotFoundError.

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.