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 Deep Learning Cookbook - Indra den Bakker
Python dict()
Python Modules
Python Set copy()
Python List remove()
Python getattr()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Create Pyramid Patterns
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python del Statement
Python dir()
Python Program to Display Calendar
Python hash()
Python input()
Python Namespace and Scope
Python Program to Iterate Through Two Lists in Parallel
Python Sets
Python eval()
Python str()
Python String strip()
Python Set issubset()
Python Program to Get the File Name From the File Path
Python Machine Learning Eqution Reference - Sebastian Raschka
Python open()
Python Object Oriented Programming
Python Program to Check If a List is Empty
Python Program to Transpose a Matrix
Python String endswith()
Python Keywords and Identifiers
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Print Output Without a Newline