Change language

Python | shutil.copyfileobj () method

|

The shutil.copyfileobj() method in Python is used to copy the contents of a file object to another file object. By default, this method copies data in chunks, and if desired, we can also specify the size of the buffer through the length parameter. 
This method copies the contents of the file from the current file position to the end of the file.

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

Parameters:
fsrc: A file-like object representing the source file to be copied
fdst: A file-like object representing the destination file, where fsrc will be copied.
length (optional): An integer value denoting buffer size.
File-like object are mainly StringIO objects, connected sockets and actual file objects.

Return Type: This method does not return any value.

Code: using method shutil.copyfileobj () to copy the contents of the source file object to the target file object

Exit :

 Contents of file object copied successfully 

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

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method

# Python program to explain the shutil.copy method fileobj ()

 
# shutil module import

import shutil

 
# Source file

source = ’file.txt’

  
# Open the source file
# in read mode and
# get the file object

fsrc = open (source, ’r’

 

 
# target file

dest = ’ file_copy.txt’

  
# Open the destination file
# in write mode and
# get file object

fdst = open (dest, ’w’ )

  

  
# Now copy the content
# file object from f1 to f2
# using the shutil.copyfileobj () method
shutil.copyfileobj (fsrc, fdst)

 
# We can also specify
  # buffer size per pause
# optional length parameter
# like shutil.copyfileobj (fsrc, fdst, 1024)

 

print ( "Contents of file object copied successfully" )

 
# Close file objects
f1.close ()
f2.close ()