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 difference_update()
Python round()
Python dict()
Python for Loop
Python Machine Learning - Sebastian Raschka
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Program to Get File Creation and Modification Date
Python String maketrans()
Python String count()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python List sort()
Python Dictionary copy()
Python Keywords and Identifiers
Python Program to Slice Lists
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python iter()
Python String split()
Python len()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python String title()
Python List append()
Python input()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Program to Check Prime Number
Python timestamp to datetime and vice-versa
Python super()
Python List count()
Python Objects and Classes
Python Numbers, Type Conversion and Mathematics
Python Program to Check If Two Strings are Anagram
Python sorted()