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

Every line of 'copy and rename file 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
this disclaimer
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
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

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
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
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
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)
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
775def rename(path, newName):
776 """
777 Renames a file or a directory to newName, in the same folder.
778
779 @since: 1.3
780
781 @type path: string
782 @param path: the docroot-path to the object to rename
783 @type newName: string
784 @param newName: the new name (basename) of the object, including extension,
785 if applicable.
786
787 @rtype: bool
788 @returns: False if newName already exists. True otherwise.
789 """
790 getLogger().info(">> rename(%s, %s)" % (path, newName))
791 if not path.startswith('/'): path = '/' + path
792
793 res = False
794 try:
795 res = FileSystemManager.instance().rename(path, newName)
796 except Exception, e:
797 e = Exception("Unable to perform operation: %s\n%s" % (str(e), Tools.getBacktrace()))
798 getLogger().info("<< rename(...): Fault:\n" + str(e))
799 raise(e)
800
801 getLogger().info("<< rename(): %s" % str(res))
802 return res
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))

Related snippets