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 sorted()
Python Program to Display Powers of 2 Using Anonymous Function
Python Package
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Get the Full Path of the Current Working Directory
Python Strings
Python Program to Find the Sum of Natural Numbers
Python if...else Statement
Python ascii()
Python String count()
Python List
Python sleep()
Python getattr()
Python Tuple index()
Python Recursion
Python Program to Iterate Over Dictionaries Using for Loop
Python Dictionary keys()
Python Set isdisjoint()
Python Program to Compute all the Permutation of the String
Python Set copy()
Python callable()
Python Data Structures and Algorithms - Benjamin Baka
Python Program to Differentiate Between type() and isinstance()
Python Program to Find Sum of Natural Numbers Using Recursion
Python Set issuperset()
Python RegEx
Python input()
Python Set difference_update()
Python repr()
Python slice()
Python List extend()
Python String startswith()