Opened 6 years ago

Closed 6 years ago

#30147 closed Cleanup/optimization (fixed)

Simplify directory creation with os.makedirs(..., exist_ok=True)

Reported by: Jon Dufresne Owned by: nobody
Component: Uncategorized Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The pattern:

if not os.path.exists(path):
    os.makedirs(path)

Can be simplified to:

os.makedirs(path, exist_ok=True

The exist_ok argument was added in Python 3.2:

https://docs.python.org/3/library/os.html#os.makedirs

The original pattern also has a potential race condition where a process could create a directory at path after the check but before the os.makedirs() call. If such a race condition were to occur, the Django process would result in a FileExistsError. os.makedirs handles this condition.

Change History (2)

comment:2 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: newclosed

In 290d8471:

Fixed #30147 -- Simplified directory creation with os.makedirs(..., exist_ok=True).

Note: See TracTickets for help on using tickets.
Back to Top