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 hex()
Python id()
Python Tuple index()
Python Program to Find Hash of File
Python help()
Python compile()
Python Program to Make a Flattened List from Nested List
Python Objects and Classes
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python String isdecimal()
Python vars()
Python isinstance()
Python Set clear()
Node.js vs Python for Backend Development
Python while Loop
Python repr()
Python Program to Remove Punctuations From a String
Python dict()
Python iter()
Python Decorators
Python Program to Create Pyramid Patterns
Python Get Current time
Python List sort()
Python Exception Handling Using try, except and finally statement
Python Program to Find Factorial of Number Using Recursion
Python Program to Find the Square Root
Python Program to Print Output Without a Newline
Python round()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python chr()
Python String casefold()
Python exec()