#30147: Simplify directory creation with os.makedirs(..., exist_ok=True)
------------------------------------------------+------------------------
Reporter: Jon Dufresne | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/30147>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.b666932ffdaa9cf255621762f3e2b4d8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.