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 Find Armstrong Number in an Interval
Python String translate()
Python Set intersection()
Python List insert()
Python Tuple count()
Python Program to Display Powers of 2 Using Anonymous Function
Python Dictionary update()
Python vars()
Python String split()
Python hash()
Python Directory and Files Management
Debug a JavaMail Program
Python break and continue
Python Program to Count the Number of Each Vowel
Python id()
Python Operators
Python Errors and Built-in Exceptions
Python super()
Python Program to Find HCF or GCD
Python sorted()
Python bin()
Python globals()
Python String capitalize()
Python isinstance()
Python Program to Convert Celsius To Fahrenheit
Python Object Oriented Programming
Python Data Structures and Algorithms - Benjamin Baka
Python next()
How to Get Started With Python?
Python Generators
Python type()
Python List pop()