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 Set pop()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Dictionary items()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Return Multiple Values From a Function
Python String isalpha()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python property()
Python Set discard()
Python List pop()
Python Strings
Python String maketrans()
Python Program to Check Armstrong Number
Python Tuple count()
Python setattr()
Python sorted()
Python String istitle()
Python break and continue
Python memoryview()
Python hash()
Python list()
Python Custom Exceptions
Python Program to Print Hello world!
Python Operator Overloading
Python all()
Python exec()
Python List sort()
Deep Learning with Python - Francois Cholletf
Python Program to Convert Two Lists Into a Dictionary
Python List count()
Python Type Conversion and Type Casting
Python input()