How to Copy a File with a New Name in Python?

Estimated read time 1 min read

To copy a file with a new name in Python, you can use the shutil module, which provides functions for file operations. Here’s an example of how you can copy a file with a new name:

import shutil

# Specify the source file path
source_file = 'path/to/source_file.txt'

# Specify the destination file path with a new name
destination_file = 'path/to/destination_file_new_name.txt'

# Copy the file
shutil.copy(source_file, destination_file)

In the code above, you need to specify the source_file path as the path of the file you want to copy. Then, specify the destination_file path with the desired new name and the desired destination directory. The shutil.copy() function is used to perform the file copy operation.

After executing the code, the file specified by source_file will be copied to the location specified by destination_file with the new name.

Make sure to replace 'path/to/source_file.txt' and 'path/to/destination_file_new_name.txt' with the actual file paths and names you want to use for the source and destination files.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply