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 Program to Count the Occurrence of an Item in a List
Python Program to Check If Two Strings are Anagram
Python if...else Statement
Python Program to Check Leap Year
Python for Loop
Python Program to Print Hello world!
Python Program to Remove Duplicate Element From a List
Python Program to Extract Extension From the File Name
Python Set symmetric_difference_update()
Python exec()
Python Program to Reverse a Number
Python time Module
Python List count()
Python Program to Display Calendar
Python Program to Find Sum of Natural Numbers Using Recursion
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Variables, Constants and Literals
Python List sort()
Python Program to Count the Number of Occurrence of a Character in String
Python oct()
Python String swapcase()
Python Program to Convert Kilometers to Miles
Deep Learning in Python - LazyProgrammer
Python Program to Get the Last Element of the List
Python Program to Merge Mails
Python Program to Represent enum
Python Program to Print Colored Text to the Terminal
Python locals()
Python String encode()
Python pow()
Python Set update()
Python tuple()