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

Every line of 'python copy file and rename' 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
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)
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
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
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
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)
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
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
170def copy_file(src, dst):
171 """ Implement the behaviour of "shutil.copy(src, dst)" without copying the
172 permissions (this was causing errors with directories mounted with samba)
173
174 Positional arguments:
175 src - the source of the copy operation
176 dst - the destination of the copy operation
177 """
178 if isdir(dst):
179 _, base = split(src)
180 dst = join(dst, base)
181 copyfile(src, dst)

Related snippets