How can you safely create a nested directory using Python?
For example, you want to create a directory called
innerDirectory inside a directory called
outerDirectory. But first you want to make sure that:
innerDirectory doesn’t already exist.
outerDirectory already exists. If
outerDirectory doesn’t exists, then Python should create it.
From Python 3.5, we can use the
pathlib module to create, delete, and manipulate files and directories easily.
pathlib module has several classes, but for the purpose of creating directories (and files), we just need the
Path class. We can import it from
pathlib like so:
>>> from pathlib import Path
Next, we need to create a
Path object. There are multiple ways to create a path object. For our purpose, we will create it using the string representation of the required path.
Since the backslash character is also used to escape characters, it is best to use the raw string literal (the string prepended with an
r) to represent the Windows path:
>>> p = Path(r"C:\Users\username\projects\outerDirectory\innerDirectory")
If you are on a Linux machine or using macOS, your path would look like:
>>> p = Path('/home/username/projects/outerDirectory/innerDirectory')
We can check if the path, including the
innerDirectory, already exists by using the
exists() method. If the path doesn’t exist it will return a
>>> p = Path(r"C:\Users\username\projects\outerDirectory\innerDirectory") >>> p.exists() False
To create the
innerDirectory we can use the
>>> p = Path(r"C:\Users\username\projects\outerDirectory\innerDirectory") >>> p.mkdir()
Let’s see what happens if the two checks in question fail.
If we use the
mkdir() method while the
innerDirectory already exists, we will get the
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\pathlib.py", line 1173, in mkdir self._accessor.mkdir(self, mode) FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\username\\projects\\outerDirectory\\innerDirectory'
If we do not want to get the error, we can set the
exist_ok argument in the
mkdir method to
True, like so:
The default value of
exist_ok is set to
False, which means that by default we will always get a
FileExistsError if the file or directory we are trying to create already exists.
If we try to create the
innerDirectory without creating an
outerDirectory first, we will get the
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\pathlib.py", line 1173, in mkdir self._accessor.mkdir(self, mode) FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\username\\projects\\outerDirectory\\innerDirectory'
If we want Python to create the missing parent directory, we can pass the value
True to the
parents argument of the
>>> p.mkdir(exist_ok=True, parents=True)
The default value of the
parents argument is also set to
False, which means that by default Python will not create a missing parent directory.
pathlib module is the most robust way of manipulating files, and the tool we recommend. But if you are working with a Python version lower than 3.5, you may need to use the