How do I copy a file in python?


One of the most prevalent activities in modern software development is copying files programmatically. In today's quick tutorial, we'll look at a few different ways to transfer files in Python using the shutil module.

Shutil is a Python Standard Library module that provides a wide range of high-level file operations. Now, when it comes to file copying, the library provides a variety of options based on whether you want to copy metadata or file permissions, as well as if the destination is a directory. It falls within the umbrella of Python's basic utility modules. This module aids in the automation of file and directory copying and removal.

shutil.copy

The shutil.copy() method copies a specified source file (without metadata) to a specified destination file or directory and returns the path to the newly produced file. The src parameter might be a string or a path-like object.

Syntax

The following is the syntax for the copy method available in shutil library.

Shutil.copy(source_file, destination_file)

Example 1

In this example, we will look into copying a file into another file using the shutil library.

#program to copy a file using python
#importing the python library shutil
import shutil
#copying the file example.txt into example2.txt
shutil.copy('example.txt', 'example2.txt')
print("The file is copied")

Output

The output produced for the following code is as follows.

The file is copied

Shutil.copy2()

Shutil.copy2() is identical to shutil.copy(), with the exception that copy2() also attempts to preserve file metadata.

Syntax

The syntax of the shutil.copy2 method in the shutil library is given below.

shutil.copy2(source, destination, *, follow_symlinks = True)

Parameters of the copy2 method −

  • source − A string containing the source file's location.

  • destination − A string containing the file or directory's destination's path.

  • if follow symlinks is passed True − This parameter's default value is True. It tries to copy all metadata from the source symbolic link to the freshly generated destination symbolic link if it is False and source represents a symbolic link. This feature is platform-specific.

  • Return Type − The path to a newly generated file is returned by this procedure as a string.

Example 2

In this example, we will understand the working of the copy2 method in the shutil library.

#program to copy a file using python
#importing the python library shutil
import shutil
#copying the file example.txt into example2.txt
shutil.copy2('example.txt', 'example2.txt')
print("The file is copied")

Output

The output produced for the above code is as follows.

The file is copied

shutil.copyfileobj

Shutil.copyfileobj is the way to go if you need to work with File Objects. The method basically copies the contents of the source file object to the destination file-like object. You can also choose a length that matches to the size of the buffer used to transfer the data. File permissions are not preserved, and directory cannot be the destination. Metadata is not copied.

Syntax

The following is the syntax for the shutil.copyfileobj method.

shutil.copyfileobj(fsrc, fdst[, length])

The parameters of the shutil.copyfileobj are as follows.

  • Fsrc − A file-like object that represents the copied source file

  • Fdst − A file-like object known as fdst that represents the file to which fsrc will be transferred.

  • length − An optional integer value indicating the size of the buffer.

Example 3

Here, we will understand how to use the copyfileobj method available in the shutil library. This method like mentioned above is used to copy the contents of the source file object to the destination file like object.

#program to copy a file using python
#importing the python library shutil
import shutil
#copying the file example.txt into example2.txt
source_file = open('example.txt', 'rb')
dest_file = open('example2.txt', 'wb')
shutil.copyfileobj(source_file, dest_file)
print("The file is copied")

Output

The output for the above code is displayed below.

The file is copied

os.system() method

The system() method in the subshell allows you to run any OS command or script in an instant.

The command or script must be passed as an argument to the system() function. This method uses the standard C library function internally. Its return value is the command's exit status.

Syntax

os.system(command)

Parameters of the system() method are mentioned below.

command − It is of string type that tells which command to execute.

Example 4

In this example, we will see how to copy a file using the os.system() method. The same is shown below. We import the required library and then use the system method to pass the command, 'copy example.txt example2.txt' in the form of a string to the system method.

#program to copy a file using python
#importing the python library OS
import os
#copying example.txt into example2.txt
os.system('copy example.txt  example2.txt')

Output

The output for the above code is displayed below.

sh: 1: copy: not found

Updated on: 11-May-2023

384 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements