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 Program to Iterate Through Two Lists in Parallel
Python isinstance()
Python strftime()
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Convert String to Datetime
Python bool()
Python Program to Add Two Numbers
Python Machine Learning - Sebastian Raschka
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Dictionary values()
Python String startswith()
Python Set issubset()
Python Program to Create a Countdown Timer
Python compile()
Python Functions
Python input()
Python Program to Catch Multiple Exceptions in One Line
Python Program to Display Calendar
Python Matrices and NumPy Arrays
Python Program to Swap Two Variables
Python next()
Python Package
Python List append()
Python divmod()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Program to Find ASCII Value of Character
Python Type Conversion and Type Casting
Python Program to Get Line Count of a File
Python Program to Create Pyramid Patterns
Python frozenset()
Debug a JavaMail Program
Python Program to Find the Factors of a Number