10 examples of 'copy and rename file in python' in Python

Every line of 'copy and rename file in python' 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
64def rename(file, newFileName):
65 DBG("VFS rename({}, {})".format(file, newFileName))
66 try:
67 os.rename(file, newFileName)
68 except:
69 return False
70 return True
50def rename_file(old_filename, new_filename):
51 full_path, filename = os.path.split(old_filename)
52 filename, extension = os.path.splitext(filename)
53 temp_filename = os.path.join(full_path, new_filename + extension)
54 os.rename(old_filename, temp_filename)
55 return temp_filename
32def move_file(file_name, new_name):
33
34 print("Moving File '{}' to '{}'".format(file_name, new_name))
35
36 # Try to rename the file
37 try:
38 os.rename(file_name, new_name)
39 except OSError as e:
40 print("Error moving file")
41 print(e)
21def rename():
22 relative_filenames = read_file(arguments.input_file)
23
24 index = 0
25 for relative_filename in relative_filenames:
26 if "domain_rgb" in relative_filename:
27 relative_filename = relative_filename.replace("domain_rgb", "domain_depth")
28 input_filename = os.path.join(arguments.input_path, relative_filename)
29 output_filename = os.path.join(arguments.output_path, arguments.output_format.format(index))
30 shutil.copy(input_filename, output_filename)
31 index += 1
23def rename(file, newFileName):
24 """Renames a file, returns true/false.
25
26 file: string - file to rename
27 newFileName: string - new filename, including the full path
28
29 Example:
30 success = xbmcvfs.rename(file,newFileName)"""
31 return bool
316def _rewrite_and_copy(src_file, dst_file, project_name):
317 """Replace vars and copy."""
318 # Create temp file
319 fh, abs_path = mkstemp()
320
321 with io.open(abs_path, 'w', encoding='utf-8') as new_file:
322 with io.open(src_file, 'r', encoding='utf-8') as old_file:
323 for line in old_file:
324 new_line = line.replace('#{project}', project_name). \
325 replace('#{project|title}', project_name.title())
326 new_file.write(new_line)
327
328 # Copy to new file
329 shutil.copy(abs_path, dst_file)
330 os.close(fh)
94def copy_file(from_file, to_file):
95 if os.path.exists(to_file):
96 run(['rm', to_file])
97 run(['cp', '-p', from_file, to_file])
44def copy_wo_overwrite(dir_, file_to_copy):
45 basename = os.path.basename(file_to_copy)
46 i = 0
47 basename, ending = os.path.splitext(basename)
48 basename = basename + "_run{}" + ending
49 while True:
50 if os.path.isfile(
51 os.path.join(dir_, basename.format(i))):
52 i += 1
53 continue
54 else:
55 copyfile(file_to_copy,
56 os.path.join(dir_, basename.format(i)))
57 break
9def install_file(source_filename, dest_filename):
10 # do not overwrite network configuration if it exists already
11 # https://github.com/evilsocket/pwnagotchi/issues/483
12 if dest_filename.startswith('/etc/network/interfaces.d/') and os.path.exists(dest_filename):
13 print("%s exists, skipping ..." % dest_filename)
14 return
15
16 print("installing %s to %s ..." % (source_filename, dest_filename))
17 try:
18 dest_folder = os.path.dirname(dest_filename)
19 if not os.path.isdir(dest_folder):
20 os.makedirs(dest_folder)
21
22 shutil.copyfile(source_filename, dest_filename)
23 except Exception as e:
24 print("error installing %s: %s" % (source_filename, e))
245def _rename_file(source, destination):
246 """Rename a single file object"""
247 try:
248 os.rename(source, destination)
249 except OSError as err:
250 raise IOError(err)

Related snippets