10 examples of 'python create file in subdirectory' in Python

Every line of 'python create file in subdirectory' 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
30def create_dir(path):
31 """Create supplied directory"""
32 command = ["mkdir", "-p", path]
33 subprocess.Popen(command).communicate()
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

76def create(self, dir_, content):
77 """Write ``content`` to a file using ``dir_`` as file-system directory.
78
79 :return: File-system path of the file that was created.
80 """
81 p = Path(dir_).joinpath(self.relpath)
82 if not p.parent.exists():
83 p.parent.mkdir(parents=True)
84 with open(p.as_posix(), 'wb') as fp:
85 if isinstance(content, str):
86 content = content.encode('utf8')
87 fp.write(content)
88 return p.as_posix()
34def create(self, path, contents):
35 dname = os.path.dirname(path)
36 if len(dname) > 0 and not os.path.isdir(dname):
37 os.makedirs(dname)
38
39 with open(path, 'w+') as fd:
40 fd.write(contents)
91def create_dir_for_file(file_path, dir_description='directory'):
92 '''
93 Creates the directory of the file_path.
94 '''
95 directory = os.path.dirname(os.path.abspath(file_path))
96 try:
97 os.makedirs(directory, exist_ok=True)
98 except Exception as e:
99 sys.exit('Error: Could not create {}: {} {}'.format(dir_description, sys.exc_info()[0].__name__, e))
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)
11def create_dir(num_files, dict_generator, extensions):
12 tmp_dir = tempfile.mkdtemp()
13 p = Path(tmp_dir)
14 for i in range(0, num_files):
15 d = dict_generator(i)
16 # last one should cause problems
17 for extension, op in extensions:
18 f_name = f"file_{i}.{extension}"
19 with open(p / f_name, "w") as f:
20 op(d, f)
21 return p
22def create():
23 logging.info('building port: cocos2d v3')
24 logging.warn('cocos2d: library is experimental, do not expect that it will work out of the box')
25
26 cocos2d_src = os.path.join(ports.get_dir(), 'cocos2d')
27 cocos2d_root = os.path.join(cocos2d_src, 'Cocos2d-' + TAG)
28 cocos2dx_root = os.path.join(cocos2d_root, 'cocos2dx')
29 cocos2dx_src = make_source_list(cocos2d_root, cocos2dx_root)
30 cocos2dx_includes = make_includes(cocos2d_root, cocos2dx_root)
31
32 cocos2d_build = os.path.join(ports.get_build_dir(), 'cocos2d')
33 shutil.copytree(os.path.join(cocos2d_root, 'samples', 'Cpp'),
34 os.path.join(cocos2d_build, 'samples'))
35
36 commands = []
37 o_s = []
38 for src in cocos2dx_src:
39 o = os.path.join(cocos2d_build, 'Cocos2d-' + TAG, 'build', src + '.o')
40 shared.safe_ensure_dirs(os.path.dirname(o))
41 command = [shared.PYTHON,
42 shared.EMCC,
43 os.path.join(cocos2dx_root, 'proj.emscripten', src),
44 '-Wno-overloaded-virtual',
45 '-Wno-deprecated-declarations',
46 '-D__CC_PLATFORM_FILEUTILS_CPP__',
47 '-DCC_ENABLE_CHIPMUNK_INTEGRATION',
48 '-DCC_KEYBOARD_SUPPORT',
49 '-DGL_ES=1',
50 '-DNDEBUG', # '-DCOCOS2D_DEBUG=1' 1 - error/warn, 2 - verbose
51 '-DCP_USE_DOUBLES=0',
52 '-O2',
53 '-s', 'USE_ZLIB=1',
54 '-s', 'USE_LIBPNG=1',
55 '-o', o, '-w']
56
57 for include in cocos2dx_includes:
58 command.append('-I' + include)
59
60 if src.endswith('.cpp'):
61 command.append('-std=c++11')
62
63 commands.append(command)
64 o_s.append(o)
65 shared.safe_ensure_dirs(os.path.dirname(o_s[0]))
66 ports.run_commands(commands)
67 final = os.path.join(cocos2d_build, libname)
68 ports.create_lib(final, o_s)
69 return final
506def create_directory(self):
507 """
508 Creates a directory under the selected directory (if the selected item
509 is a file, the parent directory is used).
510 """
511 src = self.get_current_path()
512 name, status = QtWidgets.QInputDialog.getText(
513 self.tree_view, _('Create directory'), _('Name:'),
514 QtWidgets.QLineEdit.Normal, '')
515 if status:
516 fatal_names = ['.', '..']
517 for i in fatal_names:
518 if i == name:
519 QtWidgets.QMessageBox.critical(
520 self.tree_view, _("Error"), _("Wrong directory name"))
521 return
522
523 if os.path.isfile(src):
524 src = os.path.dirname(src)
525 try:
526 os.makedirs(os.path.join(src, name), exist_ok=True)
527 except OSError as e:
528 QtWidgets.QMessageBox.warning(
529 self.tree_view, _('Failed to create directory'),
530 _('Failed to create directory: %s'), str(e))
168def make(path):
169 # Read make template into a Template object.
170 make_template = open(TEMPLATE, 'r')
171 template_data = make_template.read()
172 make_template.close()
173 template = Template(template_data)
174
175 directories = []
176
177 # Generate Makefile for all samples assuming in base ic directory.
178 if path == 'all':
179 run_dir = os.path.abspath(os.path.join('.', 'public/run/samples'))
180 step_dir = os.path.abspath(os.path.join('.', 'public/step/samples'))
181 dvt_dir = os.path.abspath(os.path.join('.', 'dvt/samples'))
182
183 for d in os.listdir(run_dir):
184 run_d = os.path.join(run_dir, d)
185 step_d = os.path.join(step_dir, d)
186
187 if os.path.isdir(step_d):
188 directories.append(step_d)
189 if os.path.isdir(run_d):
190 directories.append(run_d)
191
192 for d in os.listdir(dvt_dir):
193 sample_dir = os.path.join(dvt_dir, d)
194
195 if os.path.isdir(sample_dir):
196 directories.append(sample_dir)
197
198 else:
199 directories.append(os.path.abspath(path))
200
201 for directory in directories:
202 data = generate_makefile(directory, template)
203 target_makefile = os.path.join(directory, 'Makefile')
204 log.info("Generating Makefile in %s" % target_makefile)
205 f = open(target_makefile, 'wb')
206 f.write(data)
207 f.close()
208
209 log.info("Done")
4def create_dir(directory):
5 if not os.path.exists(directory):
6 os.makedirs(directory)

Related snippets