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 List count()
Python String maketrans()
Python Program to Find the Sum of Natural Numbers
Python String capitalize()
Deep Learning with Python - Francois Cholletf
Python globals()
Python compile()
Python Program to Convert Two Lists Into a Dictionary
Python String isprintable()
Python Program to Safely Create a Nested Directory
Python Program to Display Powers of 2 Using Anonymous Function
Python print()
Python Program to Check Whether a String is Palindrome or Not
Python list()
Python String replace()
Python min()
Java Program to Implement the Program Used in grep/egrep/fgrep
Python Program to Return Multiple Values From a Function
Machine Learning with Python for everyone - Mark E.Fenner
Python List remove()
Python Program to Solve Quadratic Equation
Python set()
Python List Comprehension
Python String expandtabs()
Python Modules
Python oct()
Python Program to Find the Size (Resolution) of a Image
Python Program to Check If a List is Empty
Python all()
Python open()
Deep Learning with Python - Francois Chollet
Python List index()