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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically

# 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 ()