Naveera A.
—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:
The innerDirectory
doesn’t already exist.
The 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.
The 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 False
:
>>> p = Path(r"C:\Users\username\projects\outerDirectory\innerDirectory") >>> p.exists() False
To create the innerDirectory
we can use the mkdir()
method:
>>> p = Path(r"C:\Users\username\projects\outerDirectory\innerDirectory") >>> p.mkdir()
Let’s see what happens if the two checks in question fail.
innerDirectory
Already ExistsIf we use the mkdir()
method while the innerDirectory
already exists, we will get the FileExistsError
:
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:
>>> p.mkdir(exist_ok=True)
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.
outerDirectory
Doesn’t ExistIf we try to create the innerDirectory
without creating an outerDirectory
first, we will get the FileNotFound
error:
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 mkdir
method:
>>> 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.
Using the 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 os.path
module.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.