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 for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Get the Full Path of the Current Working Directory
Python List insert()
Python map()
Python Set symmetric_difference()
Python break and continue
Python Set symmetric_difference_update()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python String startswith()
Python Program to Append to a File
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Iterate Through Two Lists in Parallel
Python Program to Delete an Element From a Dictionary
Python Decorators
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Remove Duplicate Element From a List
Python bin()
Python Program to Find the Square Root
Python any()
Python Program to Find the Size (Resolution) of a Image
Python Program to Display Calendar
Python Program to Check Whether a String is Palindrome or Not
Python strptime()
Python Variables, Constants and Literals
Python String isupper()
Python Directory and Files Management
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Set issubset()
Python Dictionary fromkeys()
Python Sets
Python format()