Last updated: Apr 8, 2024
Reading time·2 min
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)
.
Here is an example of how the error occurs.
import os dir_name = 'my_dir' # ⛔️ FileExistsError: [Errno 17] File exists: 'my_dir' os.makedirs(dir_name)
The my_dir
directory already exists, so a FileExistsError
is raised.
True
when calling makedirs()One way to resolve the error is to set the exist_ok
keyword argument to
True
.
import os dir_name = 'my_dir' os.makedirs(dir_name, exist_ok=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.
Alternatively, you can use a
try/except statement to
handle the FileExistsError
.
import os dir_name = 'my_dir' try: os.makedirs(dir_name) except FileExistsError: print('The directory already exists')
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.
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.
If you need to check if a path exists, use the os.path.exists()
method.
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))
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.
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.
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
.