Python | shutil.copyfile () method



The shutil.copyfile() method in Python is used to copy the contents of the source file to the target file. File metadata is not copied.  Source and destination must represent a file, and destination must be writable. If the destination already exists, it will be replaced by the original file, otherwise a new file will be created. 
If source and destination represent the same file, a SameFileError exception will be raised .

Syntax: shutil.copyfile (source, destination, *, follow_symlinks = True)

Parameter:
source : A string representing the path of the source file.
destination : A string representing the path of the destination file.
follow_symlinks (optional): The default value of this parameter is True. If False and source represents a symbolic link then a new symbolic link will be created instead of copying the file.

Note: The `*` in parameter list indicates that all following parameters (Here in our case `follow_symlinks`) are keyword-only parameters and they can be provided using their name, not as positional parameter.

Return Type: This method returns a string which represents the path of newly created file.

Code # 1: Using the shutil.copyfile () method to copy a file from the source to destination

# Python program to explain the shutil.copyfile () method

 
# import of the os module

import os

  
# shutil module import

import shutil

 
# path

path = `/ home / User / Documents`

 
# List of files and directories
# in & # 39; / home / User / Documents & # 39;

print ( " Before copying file: " )

print (os.listdir (path))

  

 
# Source path

source = " / home / User / Documents / file.txt "

  
# Destination path

destination = "/ home / User / Documents / file (copy) .txt"

 
# Copy the content
# source to destination

dest = shutil.copyfile (source, destination)

 
# List of files and directories
# in & quot; / home / User / Documents & quot;

print ( " After copying file: " )

print   (os.listdir (path))

 
# Print the path again
# generated file

print ( "Destination path:" , dest)

Exit:

 Before copying file: [`hrithik.png`,` test .py`, `sample.txt`,` file.text`, `copy.cpp`] After copying file: [` hrithik.png`, `test.py`,` sample.txt`, `file.text` , `file (copy) .txt`,` copy.cpp`] Destination path: /home/User/Documents/file(copy).txt 

Code # 2: Possible errors when using the shutil.copyfile () method

# Python program to explain the shutil.copyfile () method

  
# shutil module import

import shutil

 

 
# If source and destination
# represents the same file
# SameFileError exception
# will be raised

 
# If the destination is
i.e. directory
Exception # & # 39; IsADirectoryError & # 39;
# will be raised

 
# If destination
# not writable
Exception # Per missionError
# will be raised

 

 
# Source path

source = "/ home / User / Documents / file.txt"

 
# Destination path

destination = "/ home / User / Documents / file.txt"

 
# Copy the content
# source to destination
shutil.copyfile (source, destintion)

Output:

 Traceback (most recent call last): File "copy.py", line 31, in shutil.copyfile (source, destination ) File "/usr/lib/python3.6/shutil.py", line 104, in copyfile raise SameFileError ("{! R} and {! R} are the same file" .format (src, dst)) shutil. SameFileError: `/ home / User / Documents / file.txt` and` / home / User / Documents / file.txt` are the same file 

Code # 3: Processing errors when using the shutil.copyfile () method

# Python program to explain the shutil.copyfile () method

 
# import shutil module

import shutil

 
# Source path

source = "/ home / User /Documents/file.txt"

 
# Destination path

destination = "/ home / User / Documents"

 
# Copy the content
# source to destination

 

try :

shutil.copyfile (source, destination)

print ( "File copied successfully." )

 
# If source and destination are the same

except shutil.SameFileError:

  print ( "Source and destination represents the same file. " )

  
# If the destination is a directory.

except IsADirectoryError:

print ( "Destination is a directory." )

 
# If there are any permission issues

except PermissionError:

  print ( "Permission denied." )

  
# For other errors

except :

  print ( "Error occurred while copying file." )

Exit:

 Destination is a directory. 

Link: https://docs.python.org/3/ library / shutil.html