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 Tuple count()
Python String isupper()
Python Dictionary items()
Python Program to Find Factorial of Number Using Recursion
Python Program to Convert Celsius To Fahrenheit
Python String rsplit()
Python Program to Get File Creation and Modification Date
Python Program to Find Numbers Divisible by Another Number
Python Program to Generate a Random Number
Python Set update()
Python zip()
Deep Learning with Python - Francois Chollet
Python Program to Find the Factors of a Number
Python while Loop
Python float()
Python classmethod()
Python Program to Append to a File
Python repr()
Python enumerate()
Python Set symmetric_difference_update()
Node.js vs Python for Backend Development
Python Global, Local and Nonlocal variables
Python Machine Learning Eqution Reference - Sebastian Raschka
Python pass statement
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Program to Find Hash of File
Python Set pop()
Python RegEx
Python Set difference_update()
Python Program to Differentiate Between del, remove, and pop on a List
Python Inheritance
Python Program to Print all Prime Numbers in an Interval