← Go Back
Likewise, this function can be used to rename.
The
On the other hand,
How to Copy or Move a File
To copy a file from one location to another, use theshutil.copy()
standard function:import shutil
# Copy the file from the current location to the
# "Documents" folder.
shutil.copy("file.txt", "Documents/file.txt")
Likewise, this function can be used to rename.
shutil.copy("old-name.txt", "new-name.txt")
The
shutil.copy2()
function operates in a similar way but also attempts to copy the file's metadata.On the other hand,
shutil.move()
takes the same arguments but removes the file from its original location.# Move the file from the current location to the
# "Documents" folder.
shutil.move("file.txt", "Documents/file.txt")
🐍 You might also find interesting: