Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I safely create a nested directory in Python?

What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:

import os  file_path = "/my/directory/filename.txt" directory = os.path.dirname(file_path)  try:     os.stat(directory) except:     os.mkdir(directory)  f = file(filename) 

Somehow, I missed os.path.exists (thanks kanja, Blair, and Douglas). This is what I have now:

def ensure_dir(file_path):     directory = os.path.dirname(file_path)     if not os.path.exists(directory):         os.makedirs(directory) 

Is there a flag for open(), that makes this happen automatically?

like image 635
Parand Avatar asked Nov 07 '08 18:11

Parand


People also ask

How do I create a sub directory in Python?

To create multi-level subdirectories the makedirs() method comes into play. Actually, makedirs() is implemented in such a way that it calls mkdir() to create one directory after the next. As a parameter makedirs() accepts the entire path to be created. This method is similar to the UNIX/Linux command mkdir -p .

How do you create directory in Python if it does not exist?

import os path = '/Users/krunal/Desktop/code/database' os. makedirs(path, exist_ok=False) print("The new directory is created!") So that's how you easily create directories and subdirectories in Python with makedirs(). That's it for creating a directory if not exist in Python.

What is a nested directory?

A folder stored within another folder. Technically, the nested folder is a "subfolder," and subfolders can also contain subfolders and so on up to a maximum level.


1 Answers

On Python ≥ 3.5, use pathlib.Path.mkdir:

from pathlib import Path Path("/my/directory").mkdir(parents=True, exist_ok=True) 

For older versions of Python, I see two answers with good qualities, each with a small flaw, so I will give my take on it:

Try os.path.exists, and consider os.makedirs for the creation.

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

As noted in comments and elsewhere, there's a race condition – if the directory is created between the os.path.exists and the os.makedirs calls, the os.makedirs will fail with an OSError. Unfortunately, blanket-catching OSError and continuing is not foolproof, as it will ignore a failure to create the directory due to other factors, such as insufficient permissions, full disk, etc.

One option would be to trap the OSError and examine the embedded error code (see Is there a cross-platform way of getting information from Python’s OSError):

import os, errno  try:     os.makedirs(directory) except OSError as e:     if e.errno != errno.EEXIST:         raise 

Alternatively, there could be a second os.path.exists, but suppose another created the directory after the first check, then removed it before the second one – we could still be fooled.

Depending on the application, the danger of concurrent operations may be more or less than the danger posed by other factors such as file permissions. The developer would have to know more about the particular application being developed and its expected environment before choosing an implementation.

Modern versions of Python improve this code quite a bit, both by exposing FileExistsError (in 3.3+)...

try:     os.makedirs("path/to/directory") except FileExistsError:     # directory already exists     pass 

...and by allowing a keyword argument to os.makedirs called exist_ok (in 3.2+).

os.makedirs("path/to/directory", exist_ok=True)  # succeeds even if directory exists. 
like image 75
Blair Conrad Avatar answered Oct 09 '22 07:10

Blair Conrad