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 Set symmetric_difference()
Python Program to Safely Create a Nested Directory
Python oct()
Python Program to Merge Two Dictionaries
Python Statement, Indentation and Comments
Python Dictionary setdefault()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Set issubset()
Python String join()
Python Object Oriented Programming
Python Program to Represent enum
Python Program to Get Line Count of a File
Python String rfind()
Python String zfill()
APIs in Node.js vs Python - A Comparison
Python String format()
Python Set pop()
Python Program to Create Pyramid Patterns
Python Program to Sort a Dictionary by Value
Python complex()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Deep Learning in Python - LazyProgrammer
Python for Loop
Python String strip()
Python @property decorator
Machine Learning with Python for everyone - Mark E.Fenner
Python Closures
Python vars()
Python Program to Reverse a Number
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Concatenate Two Lists