Copying File Content in Python

In this tutorial, I will show you a few ways of copying the content of one file to another by using the Python shutli module.

The shutli module has a few functions for copying the source file content into the destination file.

The methods include:

  • copyfile
  • copyfileobj
  • copy
  • cop2

Each of these functions with example code are explained below.

The copyfile function

The copyfile function copies the content of the source file into the destination file without the metadata. The general syntax for using the copyfile method is:

shutil.copyfile(src, dst, *, follow_symlinks=True)
Parameters  Description
src There, the src argument specifies the path of the source file (from where you want to copy the content).
dst_file The dst_file specifies the destination file path.
follow_symlinks The default value of follow_symlinks parameter is True.
If value is set as False and the source represents a symbolic link then a new symbolic link will be created instead of copying the file.

The example below shows copying the content of “python-demo.txt” file into the “dest-demo.txt” file.

The dest-demo.txt was empty before we executed this code and both files are placed in the same directory where the code file exists.

Before executing the script:

Python copyfile

The code executed with copyfile method:

# copying content by shutli copyfile method

from shutil import copyfile

copyfile("python-demo.txt", "dest-demo.txt")

The file’s content after:

Python file copied

A few points to be noted about the copyfile method:

  • It copies the content of source file but no metadata. The meta-data can include your company or organization name, computer name, revisions or versions, author name etc.
  • A SameFileError occurs if you provide same source and destination files.
  • An OSError exeception is raised if the destination location is not writeable.

Using copy function

If you require copying the file content along with certain meta-data information then use the copy function.

However, the copy function only copies the permission mode of the file along with the content.

It does not copy file creation time or modification (meta-data). For that, you may use the copy2 method (see next section).

The general syntax for using the copy method is:

shutil.copy(src_file, dst_file, *, follow_symlinks=True)

You can see, the arguments are the same as the copyfile method.

The example below shows the code for copying a source file to the destination file:

# Using copy method of shutli

import shutil

shutil.copy("python-demo.txt", "dest-demo.txt")

Just like the above example, I copied the content from python-demo.txt to the empty dest-demo.txt file.

Note that if dest-demo.txt file did not exist as we executed this code then a new file with the name dest-demo.txt would have been created with the content and permission mode of the python-demo.txt file.

The example of copy2 function

As mentioned earlier, if you also require meta-data that includes the creation, and modification of the source file along with file permission and content then use the copy2 method of the shutli module.

The syntax is the same as we used for the copy function i.e.:

shutil.copy2(src, dst, *, follow_symlinks=True)

The example code shows how to use this function:

# Using copy2 method of shutli module

import shutil

shutil.copy2("demo-copy2.txt", "dest-demo-copy2.txt")

An example of copying an Excel file from some other directory

Suppose, we have an Excel file (test-excel.xlsx) that is stored in the “C:/ directory”.

We want to copy it to the “D:/test directory”.

The test directory does not contain that Excel file or is empty.

For copying the Excel file by the copy2 method, the code is:

# Copy Excel file from one directory to other

import shutil

shutil.copy2("C:/test-excel.xlsx", "D:/test/")

The example of using copyfileobj function

The general syntax of using the copyfileobj method is:

shutil.copyfileobj(fsource, fdestiny[, length])
Parameters  Description
fsource The fsource specify the file-like objects.
fdestiny The fdestiny specify the file-like objects.
length The length argument is the difference than we used in the above functions.
It specifies the buffer size and is provided as an integer.

A few important points:

  • To avoid uncontrolled memory consumption, the copyfileobj method reads file data in chunks.
  • However, if you provide a negative length value, that means not to loop over source file data in chunks.
  • The copyfileobj is the base function; all other functions we used in the above section call this method at some point.
  • This method is particularly useful for copying large files. The default buffer size is 16Kb. By using copyfileobj, you may specify a bigger buffer size that other copy methods do not allow.

The example code below shows copying an Excel file from one drive’s folder to another by using the copyfileobj method:

# Copy Excel file by copyfileobj method

import shutil

src_file = "C:/test-excel.xlsx"
dst_file = "D:/test/test-excel.xlsx"

buf_size = 30

with open(src_file, 'rb') as src:

  with open(dst_file, 'wb') as dest:

    shutil.copyfileobj(src, dest, buf_size)

src.close()

dest.close()

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!