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 Objects and Classes
Python Program to Convert Bytes to a String
Python String isprintable()
Python Program Read a File Line by Line Into a List
Python Type Conversion and Type Casting
Python String maketrans()
Python List extend()
Deep Learning in Python - LazyProgrammer
Python int()
Python Tuple
Python String format_map()
Python time Module
Python Program to Remove Punctuations From a String
Python String lstrip()
Python Deep Learning Cookbook - Indra den Bakker
Python ascii()
Python String isnumeric()
Python List index()
Python isinstance()
Python exec()
Python Program to Generate a Random Number
Python issubclass()
Python Dictionary fromkeys()
Python Anonymous / Lambda Function
Python str()
Python id()
Python tuple()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Merge Two Dictionaries
Python Program to Count the Number of Digits Present In a Number
Python object()
Python memoryview()