Copying a File in Python

Copying a File in Python
(Last Updated On: 17/05/2023)

Python Program to Copying a File Using shutil.copyfile()

# Python Program to Copying a File Using shutil.copyfile()
# shutil.copyfile() method is used to copy the content of source file to destination file

# importing shutil module
import shutil

# path
path = '/home/User/Documents'

# Source path
source = "/home/User/Documents/one.txt"

# Destination path
destination = "/home/User/Documents/one_copy.txt"

# Copy the content of source to destination
dest = shutil.copyfile(source, destination)

# Print path of newly created file
print("Destination path:", dest)

Output:

Destination path: /home/User/Documents/one_copy.txt

Python Program to Copying a File Using shutil.copy2()

# Python Program to Copying a File Using shutil.copy2()
# shutil.copy2() method also preserves the metadata of file

# importing shutil module
import shutil

# path
path = '/home/User/Documents'

# Source path
source = "/home/User/Documents/file1.txt"

# Destination path
destination = "/home/User/Documents/file1_copy.txt"

# Copy the content of source to destination
dest = shutil.copy2(source, destination)

# Print path of newly created file
print("Destination path:", dest)

Output:

Destination path: /home/User/Documents/file1_copy.txt

Python Program to Copying a File Using shutil.copyfileobj()

# Python Program to Copying a File Using shutil.copyfileobj()
# shutil.copyfileobj() method is used to copy the content of source file-like object to destination file-like object

# importing shutil module
import shutil

# Source file
source = 'file1.txt'
 
# Open the source file in read mode and get the file object
fsrc = open(source, 'r')
 
# destination file
dest = 'file1_copy.txt'
 
# Open the destination file in write mode and get the file object
fdst = open(dest, 'w')
 
 
# Now, copy the contents of file object f1 to f2 using shutil.copyfileobj() method
shutil.copyfileobj(fsrc, fdst)
 
# We can also specify the buffer size by passing optional length parameter like shutil.copyfileobj(fsrc, fdst, 1024)
   
print("Contents of file object copied successfully")
 
# Close file objects
f1.close()
f2.close()

Output:

Contents of file object copied successfully

Python Program to Copying a File Using shutil.copy()

# Python Program to Copying a File Using shutil.copy()
#shutil.copy() method is used to copy a specified source without metadata to the destination file and then return the path to the newly created file

# importing shutil module
import shutil

#path
path = '/home/User/Documents'
 
 
# Source path
source = "/home/User/Documents/file3.txt"
 
# Destination path
destination = "/home/User/Documents/file3_copy.txt"
 
# Copy the content of source to destination
dest = shutil.copy(source, destination)
 
# Print path of newly created file
print("Destination path:", dest)

Output:

Destination path: /home/User/Documents/file3_copy.txt

Leave a Reply

Your email address will not be published. Required fields are marked *