How To Copy File In Python?

Python provides different methods in order to copy files in a programmatic way. These methods have different features and attributes. Mainly the shutil module provides most of the file copy methods. In this tutorial, we examine these methods in detail with their features and attributes.

Copy File Methods In Python

In the general following, 4 methods are provided for file copy.

FunctionCopies
metadata
Copies
permissions
Uses file objectDestination
maybe directory
shutil.copyNoYesNoYes
shutil.copyfileNoNoNoNo
shutil.copy2YesYesNoYes
shutil.copyfileobjNoNoYesNo
  • Copies Metadata feature is used to copy file metadata or attributes like owner, group, last modification, last access etc.
  • Copies permissions feature is used to copy permission like user, owner, read, write and executed etc.
  • Destination maybe directory feature is used to specify the desitnation as a directory where the file is copied with its original name.

Copy File with shutil.copy() Method

The shutil.copy() method is used to copy files without metadata information. The destination can be a directory where the original name is used for the destination file name and also the permissions of the file are copied too. The syntax of the shutil.copy() method is like below.

shutil.copy(SOURCE,DESTIONATION,follow_symlinks=FOLLOW_SYMLINKS)
  • SOURCE is the source file which can be specified as a full or absolute path or relatative path.
  • DESTINATION is the destination file name or directory where if a directory is specified as destination the SOURCE file name is used for newly copied file.
  • FOLLOW_SYMLINKS is True by default where symlink files can be used in source and destination.
import shutil

#Copy data.txt into the "/home/ismail"
shutil.copy("data.txt","/home/ismail/data.txt")

Alternatively, we can only specify the destination directory or path in order to copy file with its original name.

import shutil

#Copy data.txt into the "/home/ismail"
shutil.copy("data.txt","/home/ismail/")

By default the shutil.copy() method follows the symlinks but we can prevent following symlinks by setting follow_symlink parameter as False .

import shutil

#Copy data.txt into the "/home/ismail"
shutil.copy("data.txt","/home/ismail/",follow_symlink=False)

Copy File with shutil.copyfile() Method

The shutil.copyfile() method is used to copy the source file to the specified destination file. It does not copy the attributes or permissions. Also, the destination should be specified as a file and can not be a directory. The syntax of shutil.copyfile() is like below.

shutil.copyfile(SOURCE,DESTIONATION,follow_symlinks=FOLLOW_SYMLINKS)
  • SOURCE is the source file which can be specified as a full or absolute path or relatative path.
  • DESTINATION is the destination file name or directory where if a directory is specified as destination the SOURCE file name is used for newly copied file.
  • FOLLOW_SYMLINKS is True by default where symlink files can be used in source and destination.

The shutil.copyfile() method can be used like below. The destination should be specified as a file.

import shutil

#Copy data.txt into the "/home/ismail"
shutil.copyfile("data.txt","/home/ismail/data.txt")

Copy File with shutil.copy2() Method

The shutil.copy2() file is very similar to the shutil.copy() method where the only difference is the file metadata is preserved. The syntax of the shutil.copy2() is like below.

shutil.copyfile(SOURCE,DESTIONATION,follow_symlinks=FOLLOW_SYMLINKS)
  • SOURCE is the source file which can be specified as a full or absolute path or relatative path.
  • DESTINATION is the destination file name or directory where if a directory is specified as destination the SOURCE file name is used for newly copied file.
  • FOLLOW_SYMLINKS is True by default where symlink files can be used in source and destination.

In the following example, we copy file with the shutil.copy2() method and also preserve its metadata.

import shutil

#Copy data.txt into the "/home/ismail"
shutil.copyfile("data.txt","/home/ismail/data.txt")

Alternatively, we can specify the destination as a directory without providing the destination file name. The original or source file name will be used as the destination file name.

import shutil

#Copy data.txt into the "/home/ismail"
shutil.copyfile("data.txt","/home/ismail/")

Copy File with shutil.copyfileobj() Method

The shutil.copyfileobj() method is used to files as objects. So first the source and destination file should be opened as object. The open() method is used to open source and destination files as objects. The syntax of the shutil.copyfileobj() is like below.

shutil.copyfile(SOURCE_OBJ,DESTIONATION_OBJ)
  • SOURCE_OBJ is the source file object.
  • DESTINATION is the destination file object.

The shutils.copyfileobj() method can be used like below to copy files as objects.

import shutil

source_file = open('data.txt','rb')
destination_file = open('data.txt','wb')

shutil.copyfileobj(source_file,destination_file)

Leave a Comment