In this example, you will learn to copy the content of a file to another file using Python.
To understand this example, you should have the knowledge of the following Python programming topics:
Using shutil module
from shutil import copyfile copyfile("/root/a.txt", "/root/b.txt")
The first parameter of copyfile()
is the path of the source file and the second parameter is the path of the destination file. The content of the destination file is replaced with the content of the source file.
There are other methods copy()
, cop2()
, and copyfileobj()
which serve the same purpose with some metadata changes.
Method | Preserves Permissions | Supports Directory as Destination | Copies Metadata | Supports file object |
---|---|---|---|---|
copy() | Yes | Yes | No | No |
copyfile() | No | No | No | No |
copy2() | Yes | Yes | Yes | No |
copyfileobj() | No | No | No | Yes |
Related posts:
Python String startswith()
Python Program to Illustrate Different Set Operations
Python Program to Find Armstrong Number in an Interval
Python String replace()
Python globals()
Python Operators
Python Multiple Inheritance
Python format()
Python List append()
Python Program to Find the Largest Among Three Numbers
Python Program to Remove Duplicate Element From a List
Python String zfill()
Python Set symmetric_difference_update()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Program to Make a Flattened List from Nested List
Python List remove()
Python next()
Python min()
Python Program to Check If Two Strings are Anagram
Python Program to Create a Long Multiline String
Python String isupper()
Python eval()
Python Program to Find the Sum of Natural Numbers
Python String splitlines()
Python chr()
Python Inheritance
Python Program to Slice Lists
Python Program to Swap Two Variables
Python for Loop
Python Program to Differentiate Between type() and isinstance()
Python super()
Python list()