How Can I Safely Create a Nested Directory?

Naveera A.

The Problem

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.

The Solution

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.

The innerDirectory Already Exists

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

The outerDirectory Doesn’t Exist

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

Get Started With Sentry

Get actionable, code-level insights to resolve Python performance bottlenecks and errors.

  1. Create a free Sentry account

  2. Create a Python project and note your DSN

  3. Grab the Sentry Python SDK

pip install --upgrade sentry-sdk
  1. Configure your DSN
import sentry_sdk sentry_sdk.init( "https://<key>@sentry.io/<project>", # Set traces_sample_rate to 1.0 to capture 100% # of transactions for performance monitoring. # We recommend adjusting this value in production. traces_sample_rate=1.0, )

Loved by over 4 million developers and more than 90,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.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.