Is there a Python method to create directories recursively? I have this path:
/home/dail/
I would like to create
/home/dail/first/second/third
Can I do it recursively or I have to create one directory after the other?
The same thing for:
chmod and chown can I do it recursively without assign permissions for each file/dir?
This question is related to
python
os.makedirs
is what you need. For chmod
or chown
you'll have to use os.walk
and use it on every file/dir yourself.
a fresh answer to a very old question:
starting from python 3.2 you can do this:
import os
path = '/home/dail/first/second/third'
os.makedirs(path, exist_ok=True)
thanks to the exist_ok
flag this will not even complain if the directory exists (depending on your needs....).
starting from python 3.4 (which includes the pathlib module) you can do this:
from pathlib import Path
path = Path('/home/dail/first/second/third')
path.mkdir(parents=True)
starting from python 3.5 mkdir
also has an exist_ok
flag - setting it to True
will raise no exception if the directory exists:
path.mkdir(parents=True, exist_ok=True)
I agree with Cat Plus Plus's answer. However, if you know this will only be used on Unix-like OSes, you can use external calls to the shell commands mkdir
, chmod
, and chown
. Make sure to pass extra flags to recursively affect directories:
>>> import subprocess
>>> subprocess.check_output(['mkdir', '-p', 'first/second/third'])
# Equivalent to running 'mkdir -p first/second/third' in a shell (which creates
# parent directories if they do not yet exist).
>>> subprocess.check_output(['chown', '-R', 'dail:users', 'first'])
# Recursively change owner to 'dail' and group to 'users' for 'first' and all of
# its subdirectories.
>>> subprocess.check_output(['chmod', '-R', 'g+w', 'first'])
# Add group write permissions to 'first' and all of its subdirectories.
EDIT I originally used commands
, which was a bad choice since it is deprecated and vulnerable to injection attacks. (For example, if a user gave input to create a directory called first/;rm -rf --no-preserve-root /;
, one could potentially delete all directories).
EDIT 2 If you are using Python less than 2.7, use check_call
instead of check_output
. See the subprocess
documentation for details.
Here is my implementation for your reference:
def _mkdir_recursive(self, path):
sub_path = os.path.dirname(path)
if not os.path.exists(sub_path):
self._mkdir_recursive(sub_path)
if not os.path.exists(path):
os.mkdir(path)
Hope this help!
Try using os.makedirs:
import os
import errno
try:
os.makedirs(<path>)
except OSError as e:
if errno.EEXIST != e.errno:
raise
Source: Stackoverflow.com