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 abs()
Python Program to Merge Mails
Python Program to Print the Fibonacci sequence
Python String isprintable()
Python String format()
Python dict()
Python memoryview()
Python Program to Find Hash of File
Python del Statement
Python Dictionary
Python @property decorator
Python Program to Iterate Over Dictionaries Using for Loop
Deep Learning with Applications Using Python - Navin Kumar Manaswi
APIs in Node.js vs Python - A Comparison
How to Get Started With Python?
Python Program to Check if a Number is Positive, Negative or 0
Python next()
Python locals()
Python Strings
Python Sets
Python Program to Convert String to Datetime
Python Program to Multiply Two Matrices
Python Machine Learning - Sebastian Raschka
Python Program to Safely Create a Nested Directory
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Convert Kilometers to Miles
Python open()
Debug a JavaMail Program
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Print Hello world!
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper