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
1 2 | 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 frozenset()
Python Type Conversion and Type Casting
Python Global Keyword
Python Numbers, Type Conversion and Mathematics
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Operator Overloading
Python Set discard()
Python Program to Get the File Name From the File Path
Deep Learning with Python - Francois Cholletf
Python Program to Create a Long Multiline String
Python Program to Represent enum
Python Dictionary clear()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Find the Largest Among Three Numbers
Python String casefold()
Python Anonymous / Lambda Function
Python map()
Python Dictionary keys()
Python Program to Get the Last Element of the List
Python Errors and Built-in Exceptions
Python Program to Find HCF or GCD
Python String ljust()
Python String format_map()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python all()
Python Program to Delete an Element From a Dictionary
Python String find()
Python eval()
Python String capitalize()
Python Program Read a File Line by Line Into a List
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Compute the Power of a Number