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:
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python File I/O Operation
Python Program to Randomly Select an Element From the List
Python String isupper()
Python Dictionary items()
Machine Learning with Python for everyone - Mark E.Fenner
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Program to Print Hello world!
Python Dictionary fromkeys()
Python Program to Create a Countdown Timer
Python Program to Find ASCII Value of Character
Python bool()
Python Program to Merge Two Dictionaries
Python Object Oriented Programming
Python staticmethod()
Python String capitalize()
Python type()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python bytearray()
Python Program to Delete an Element From a Dictionary
Python float()
Python Program to Check if a Key is Already Present in a Dictionary
Python Program to Differentiate Between type() and isinstance()
Python Global Keyword
Debug a JavaMail Program
Deep Learning with Python - Francois Cholletf
Python str()
Python Program to Find the Factorial of a Number
Python Shallow Copy and Deep Copy
Python Dictionary pop()
Python Numbers, Type Conversion and Mathematics
Python classmethod()