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

Every line of 'python create dir 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
21def make_dir_if_not_exists(path):
22 if not os.path.exists(path): os.makedirs(path)
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

9def create_dir(name):
10 if not Path.exists(name):
11 Path.mkdir(name)
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
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)
56def _mkdirIfNotExists(dirname):
57 if os.path.isdir(dirname):
58 return False
59 os.makedirs(dirname)
60 return True
43def _create_dir(parent: Path, name: str, mode: int) -> Path:
44 p = Path(parent, name)
45 p.mkdir()
46 p.chmod(mode)
47 return p
30def create_dir(path):
31 """Create supplied directory"""
32 command = ["mkdir", "-p", path]
33 subprocess.Popen(command).communicate()
4def create_dir(directory):
5 if not os.path.exists(directory):
6 os.makedirs(directory)
52def create_dir(path):
53 if not os.path.exists(path):
54 try:
55 print("Attempting to create directory %s.\n" % path)
56 os.makedirs(path)
57 except OSError as exc:
58 if exc.errno != errno.EEXIST:
59 raise
11def ensure_dir(dir):
12 try:
13 os.stat(dir)
14 except:
15 os.makedirs(dir)

Related snippets