How to Copy Files in Python | Towards Data Science

Copying files programmatically, is one of the most common tasks in day-to-day software development. In today’s short guide we will explore a few different ways for copying files in Python using a library called shutil.
The shutil module is part of the Python’s Standard Library and offers a wide range of high level file operations. Now with respect to file copying, the library offers numerous methods that can be used depending on whether you want to copy metadata or file permissions and whether the desired destination will be a directory.
In this article, we will discuss about all available methods for doing so, namely
shutil.copy
shutil.copyfile
shutil.copy2
shutil.copyfileobj
At the end of the guide you can find a table that summarises the features of each individual methods mentioned above.
shutil.copy() method is used to copy specified source (without the metadata) to the destination file or directory and it will return the path to the newly created file. The src can either be a path-like object or a string.
shutil.copy(src, dst, *, follow_symlinks=True)
Preserves file permissions
Destination can be a directory
Does not copy metadata
Does not work with File objects
Example
import shutil# Copy file example.txt into a new file called example_copy.txtshutil.copy('example.txt', 'example_copy.txt')# Copy file example.txt into directory test/shutil.copy('example.txt', 'test/')
The shutil.copyfile() method is used to copy the source file (without the metadata) to the specified destination file. Again, the src can either be a path-like object or a string.
shutil.copyfile(src, dst, *, follow_symlinks=True)
Does not preserve file permissions
Destination cannot be a directory
Does not copy metadata
Does not work with File objects
Example
import shutil# Copy file example.txt into a new file called example_copy.txtshutil.copyfile('source.txt', 'destination.txt')
The shutil.copy2() method is identical to shutil.copy() except that copy2() attempts to preserve file metadata as well.
shutil.copy2(src, dst, *, follow_symlinks=True)
Preserves file permissions
Destination can be a directory
Copies metadata
Does not work with File objects
Example
import shutil# Copy file example.txt into a new file called example_copy.txtshutil.copy2('example.txt', 'example_copy.txt')# Copy file example.txt into directory test/shutil.copy2('example.txt', 'test/')
Now if you have to work with File Objects then shutil.copyfileobj is the way to go. Essentially, the method will copy the contents of the source file object to the specified destination file-like object. You can also set the length that corresponds to the buffer size used to copy the contents.
shutil.copyfileobj(fsrc, fdst[, length])
Does not preserve file permissions
Destination cannot be a directory
Does not copy metadata
Can work with File objects
Example
import shutilsource_file = open('example.txt', 'rb')dest_file = open('example_copy.txt', 'wb')shutil.copyfileobj(source_file, dest_file)
In today’s short guide we explore a few different ways for programmatically copying files using Python. More precisely, we introduced a few relevant methods offered by shutil module that comes as part of the Standard Library of the language.
Depending on whether you want to copy file permissions, metadata and whether you want to copy a file to a directory (or even use a method which in accepts file objects) you need to choose the one that will do the trick for your specific use-case.
The table below summarises the capabilities of each of the methods discussed in this article. Note that the only method that accepts File Objects is shutil.copyfileobj.
+--------------------+----------------+--------------+-------------+|       Method       | Preserves File |  Destination |    Copies   ||                    |  Permissions   | can be a dir |   Metadata  |+--------------------+----------------+--------------+-------------+| shutil.copy        |        ✔       |      ✔       |      ✗      || shutil.copyfile    |        ✗       |      ✗       |      ✗      || shutil.copy2       |        ✔       |      ✔       |      ✔      || shutil.copyfileobj |        ✗       |      ✗       |      ✗      |+--------------------+----------------+--------------+-------------+
Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.
gmyrianthous.medium.com
You may also like