[Django] #30147: Simplify directory creation with os.makedirs(..., exist_ok=True)

12 views
Skip to first unread message

Django

unread,
Jan 31, 2019, 10:10:36 AM1/31/19
to django-...@googlegroups.com
#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.

Django

unread,
Jan 31, 2019, 10:16:02 AM1/31/19
to django-...@googlegroups.com
#30147: Simplify directory creation with os.makedirs(..., exist_ok=True)
-------------------------------------+-------------------------------------

Reporter: Jon Dufresne | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Uncategorized | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jon Dufresne):

* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/10919

--
Ticket URL: <https://code.djangoproject.com/ticket/30147#comment:1>

Django

unread,
Jan 31, 2019, 1:15:18 PM1/31/19
to django-...@googlegroups.com
#30147: Simplify directory creation with os.makedirs(..., exist_ok=True)
-------------------------------------+-------------------------------------

Reporter: Jon Dufresne | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Uncategorized | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"290d8471bba35980f3e228f9c171afc40f2550fa" 290d8471]:
{{{
#!CommitTicketReference repository=""
revision="290d8471bba35980f3e228f9c171afc40f2550fa"
Fixed #30147 -- Simplified directory creation with os.makedirs(...,
exist_ok=True).
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30147#comment:2>

Reply all
Reply to author
Forward
0 new messages