Last updated: Apr 13, 2024
Reading timeยท4 min
To copy files and rename them in Python:
copy()
method from the shutil
module.shutil.copy()
method.from shutil import copy src_path = 'example.txt' destination_path = 'example-new.txt' copy(src_path, destination_path) print('File copied and renamed successfully!')
The code sample assumes that you have an example.txt
file located in the same
directory as your Python script.
bobby hadz com
Running the script produces an example-new.txt
file with the same contents.
bobby hadz com
You can also pass absolute paths to the shutil.copy()
method.
The destination path can also be a directory.
The shutil.copy()
method copies the src
file to the specified destination.
If the destination path points to a file that already exists, it will be replaced.
copy()
method copies the file data and the file's permission mode, however, the method doesn't copy the file creation and modification times.If you want to preserve all file metadata from the original, use the shutil.copy2 method.
from shutil import copy2 src_path = 'example.txt' destination_path = 'example-new.txt' copy2(src_path, destination_path) print('File copied and renamed successfully!')
The copy2()
method is identical to copy()
, however, it also attempts to
preserve file metadata.
When using shutil.copy()
, if the destination path points to a file that
already exists, it will be replaced.
However, in some cases, you might need to produce unique destination filenames.
You can use the current timestamp to achieve this.
from datetime import datetime from shutil import copy2 timestamp = str(datetime.now())[:19] timestamp = timestamp.replace(':', '_') print(timestamp) src_path = 'example.txt' destination_path = f'example-{timestamp}.txt' copy2(src_path, destination_path) print(f'File copied to {destination_path}')
The code sample assumes that you have an example.txt
file in the same
directory as your Python script.
bobby hadz com
Running the code sample copies the example.txt
file to a destination that
contains the current timestamp, e.g. example-2023-08-30 12_31_06.txt
.
We used the datetime.now() method to get the current local date and time.
from datetime import datetime # ๐๏ธ 2023-08-30 12:34:38.681721 print(datetime.now())
We used string slicing to exclude the milliseconds.
from datetime import datetime # ๐๏ธ 2023-08-30 12:35:20 print(str(datetime.now())[:19])
The last step is to use the str.replace method to replace the colons with underscores.
from datetime import datetime # ๐๏ธ 2023-08-30 12_36_02 print(str(datetime.now())[:19].replace(':', '_'))
Unless you rerun the command multiple times concurrently, the destination filename will be unique.
Alternatively, you can use the time()
method from the time
module to get the
current time in seconds since the Epoch.
from time import time from shutil import copy2 timestamp = time() timestamp = str(timestamp).replace('.', '_') print(timestamp) # 1693388353_6106539 src_path = 'example.txt' destination_path = f'example-{timestamp}.txt' copy2(src_path, destination_path) print(f'File copied to {destination_path}')
Running the code sample produces the following output.
1693388421_4974587 File copied to example-1693388421_4974587.txt
If you need to copy all files from a directory to another directory and rename them, use the following code sample.
import os from shutil import copy2 from pathlib import Path def copy_and_rename(src, dest): Path(destination).mkdir(parents=True, exist_ok=True) for item in os.listdir(src): current_path = os.path.join(src, item) if os.path.isdir(current_path): copy_and_rename(current_path, dest) elif current_path.endswith(".txt"): file_name, extension = item.rsplit('.', maxsplit=1) print(file_name) print(extension) updated_filename = file_name + '-new' + '.' + extension copy2( current_path, os.path.join( destination, updated_filename ) ) src_path = 'my-directory' destination = 'new-directory' copy_and_rename(src_path, destination)
The code sample assumes that you have the following folder structure.
my-project/ โโโ main.py โโโ my-directory โโโ example-1.txt โโโ example-2.txt โโโ example.txt
Running the code sample produces a new-directory
folder that has the following
structure.
new-directory/ โโโ example-1-new.txt โโโ example-2-new.txt โโโ example-new.txt
The copy_and_rename
function takes source and destination directories, copies
the files from the source directory to the destination directory and renames
them.
The first line of the function creates the destination directory if it doesn't already exist.
Path(destination).mkdir(parents=True, exist_ok=True)
We used a for loop to iterate over the files and directories of the source directory.
for item in os.listdir(src): current_path = os.path.join(src, item)
We used an if
statement to check if each item is a directory path.
if os.path.isdir(current_path): copy_and_rename(current_path, dest)
If the path is a directory, we call the copy_and_rename()
function
recursively.
The elif
statement checks if the current item is a text file (ends with
.txt
).
elif current_path.endswith(".txt"): Path(destination).mkdir(parents=True, exist_ok=True) file_name, extension = item.rsplit('.', maxsplit=1) print(file_name) print(extension) updated_filename = file_name + '-new' + '.' + extension copy2( current_path, os.path.join( destination, updated_filename ) )
If the file is a text file, we rename it by adding the "-new"
substring at the
end and copy it using the shutil.copy2()
method.
You can learn more about the related topics by checking out the following tutorials: