If you need to copy a file using Python and don’t know how, you’ve come to the right place.
When you want to copy a file, several scenarios can occur: just copy the content, accept a target directory as a parameter, copy metadata, etc. That’s why Python offers different alternatives when copying a file. We will see here the most common ones that use the module
Copy a file in Python using the copy function
copy is the main function you should use when you want to copy a file in Python. As I said, it is defined inside the module
shutil. The key feature of this feature is that it copies the content of the file and its permissions, but does not copy other types of metadata like the original creation and modification dates.
How exactly does it work? This function defines the following parameters:
src: Full path or name of the source file. It can be a string or an object of type
dst: Destination file path. If the path is a directory, the source file is copied to the directory using the original file name as the name.
follow_symlinksis wrong and
srcis a symbolic link,
dstit is created as a symbolic link. Yes
follow_symlinksis true and
srcis a symbolic link,
dstwill be a copy of the file you are referring to
Let’s look at some application examples:
import shutil shutil.copy('source.txt', 'destination.txt') shutil.copy('/path/to/source.txt', '/path/destination/copy.txt') shutil.copy('/path/to/source.txt', '/path/destination')
Copy the metadata with copy2
If you also need to copy a file’s metadata, you can use the function
copy2. This function behaves the same as the function
copy from the previous section, with the difference that it also preserves the metadata of the original file in the target file.
import shutil shutil.copy2('source.txt', 'destination.txt') shutil.copy2('/path/to/source.txt', '/path/destination/copy.txt') shutil.copy2('/path/to/source.txt', '/path/destination')
Copy only the contents of a file
If you only want to copy the contents of a file, you can make use of the function
copyfile(src, dst, follow_symlinks=True). This function creates a copy of the original file
src in the target file
dst are strings with the full paths of the source and target files. Even if the target file already exists, it will be replaced.
Another thing to note about this function is that the target must be writable, otherwise an exception of type
import shutil shutil.copyfile('source.txt', 'destination.txt') shutil.copyfile('/path/to/source.txt', '/path/destination/copy.txt')
Copy the contents of a file using file objects
The last feature we will look at is
copyfileobj. This feature is similar to the previous one except for that
dst they must be file-like objects (not strings with source/destination paths).
import shutil file_src="source.txt" f_src = open(file_src, 'rb') file_dest="destination.txt" f_dest = open(file_dest, 'wb') shutil.copyfileobj(f_src, f_dest)
Summary of the functions of the Shutil module
|function||hold permissions||Copy other metadata||Support directory as target||Accept file objects|