How to copy a file with Python

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 shutil.

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 PathLike.
  • 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_symlinks (Originally, True): Yes follow_symlinks is wrong and src is a symbolic link, dst it is created as a symbolic link. Yes follow_symlinks is true and src is a symbolic link, dst will be a copy of the file you are referring to src.

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.

so much src What 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 OSError.

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 src and dst they must be file-like objects (not strings with source/destination paths).

import shutil

f_src = open(file_src, 'rb')

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
shutil.copy Yes no Yes no
shutil.copy2 Yes Yes Yes no
shutil.copyfile no no no no
shutil.copyfileobj no no no Yes