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 *args and **kwargs
Python Program to Get a Substring of a String
Python Dictionary
Python str()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python round()
Python Program to Find the Square Root
Python Program to Check Armstrong Number
Python Program to Merge Two Dictionaries
Python break and continue
Python getattr()
Python len()
Python set()
Python delattr()
Python Program to Get Line Count of a File
Python Program to Convert String to Datetime
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Find LCM
Python callable()
Python Dictionary update()
Python String zfill()
Python String endswith()
Python String replace()
Python Shallow Copy and Deep Copy
Python enumerate()
Python pass statement
Python Object Oriented Programming
Python Program to Display Powers of 2 Using Anonymous Function
Python setattr()
Python Program to Access Index of a List Using for Loop
Python Set isdisjoint()
Python List reverse()