10 examples of 'python create directory if not exists' in Python

Every line of 'python create directory if not exists' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
9def create_directory(directory_path, ensure_writability = False):
10 """
11 Creates a directory (with subdirs) if it doesn't exist.
12
13 @param directory_path: the path of the directory and subdirectories to be created.
14 """
15 if ensure_writability:
16 if not os.access(os.path.dirname(directory_path), os.W_OK):
17 return False
18
19 try:
20 os.makedirs(directory_path)
21 return True
22 except OSError, e:
23 if e.errno != errno.EEXIST:
24 raise
25
26 return False
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

16def createdirectory(dirpath):
17 # we don't use os.mkdir/chown because we need sudo
18 command = ['/bin/mkdir', '-p', dirpath]
19 retcode, out, err = utils.execCmd(command, sudo=True, raw=True)
20 if retcode != 0:
21 sys.stderr.write('directlun: error mkdir %s, err = %s\n' % (dirpath, err))
22 sys.exit(2)
23
24 mode = '755'
25 command = ['/bin/chmod', mode, dirpath]
26 if retcode != 0:
27 sys.stderr.write('directlun: error chmod %s %s, err = %s\n' % (dirpath, mode, err))
28 sys.exit(2)
21def make_dir_if_not_exists(path):
22 if not os.path.exists(path): os.makedirs(path)
4def create_dir(directory):
5 if not os.path.exists(directory):
6 os.makedirs(directory)
30def create_dir(path):
31 """Create supplied directory"""
32 command = ["mkdir", "-p", path]
33 subprocess.Popen(command).communicate()
9def create_dir(name):
10 if not Path.exists(name):
11 Path.mkdir(name)
16def try_make_dir(directory):
17 makedirs(directory)
109def create_directory(dirname):
110 """Create dirname, including any intermediate directories necessary to
111 create the leaf directory."""
112 # Allow group permissions on the directory we are about to create
113 old_umask = os.umask(0o007)
114 try:
115 os.makedirs(dirname)
116 except OSError, e:
117 if e.errno != errno.EEXIST or not os.path.isdir(dirname):
118 raise DatasetError('Directory %s could not be created' % dirname)
119 finally:
120 # Put back the old umask
121 os.umask(old_umask)
139def create_dir(dirname):
140 if not os.path.exists(os.path.expanduser(dirname)):
141 try:
142 os.makedirs(os.path.expanduser(dirname))
143 except (IOError, OSError):
144 pass
7def try_create_directory(path: str) -> None:
8 """
9 tries to create a directory at given path
10 Args:
11 path:
12
13 """
14 if not os.path.exists(path):
15 os.makedirs(path)

Related snippets