10 examples of 'python create directory' in Python

Every line of 'python create directory' 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
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)
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

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)
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
9def create_dir(name):
10 if not Path.exists(name):
11 Path.mkdir(name)
424def mkdir(path,root=None,mode=None):
425 '''create a new directory in the root directory
426
427 create a directory at *path* and any necessary parents (i.e. ``mkdir -p``).
428 Default mode is read/write/execute for \'user\' and \'group\', and then
429 read/execute otherwise.
430
431 Args:
432 path (str): string name of the new directory.
433 root (str, default=None): path at which to build the new directory.
434 mode (str, default=None): octal read/write permission [default: 0o775].
435
436 Returns:
437 string absolute path for new directory.
438 '''
439 if mode is None: mode = MODE
440 if not root: root = os.curdir
441 newdir = os.path.join(root,path)
442 absdir = os.path.abspath(newdir)
443 import errno
444 try:
445 os.makedirs(absdir,mode)
446 return absdir
447 except OSError:
448 err = sys.exc_info()[0]
449 if (err.errno != errno.EEXIST) or (not os.path.isdir(absdir)):
450 raise
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)
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
201def create_dir(path, **kwargs):
202 """Validate and attempt to create a directory, if it does not exist."""
203
204 # If input path is not a path object, try to make it one
205 if not isinstance(path, Path):
206 try:
207 path = Path(path)
208 except TypeError:
209 error("{p} is not a valid path", p=path)
210
211 # If path does not exist, try to create it
212 if not path.exists():
213 try:
214 path.mkdir(**kwargs)
215 except PermissionError:
216 error(
217 "{u} does not have permission to create {p}. Try running with sudo?",
218 u=os.getlogin(),
219 p=path,
220 )
221
222 # Verify the path was actually created
223 if path.exists():
224 success("Created {p}", p=path)
225
226 # If the path already exists, inform the user
227 elif path.exists():
228 info("{p} already exists", p=path)
229
230 return True
19def _create_directories(self):
20 if not os.path.exists(DATA_DIR):
21 print('Creating directory:{0}'.format(DATA_DIR))
22 os.makedirs(DATA_DIR)
23
24 if not os.path.exists(DELGADO_DIR):
25 print('Creating directory:{0}'.format(DELGADO_DIR))
26 os.makedirs(DELGADO_DIR)
27
28 if not os.path.exists(EXPERIMENTS_TRAINED_MODELS_DIR):
29 print('Creating directory:{0}'.format(EXPERIMENTS_TRAINED_MODELS_DIR))
30 os.makedirs(EXPERIMENTS_TRAINED_MODELS_DIR)

Related snippets