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 Inheritance
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Compute all the Permutation of the String
Python Program to Concatenate Two Lists
Python sum()
Python strptime()
Python bin()
Python Program to Print Output Without a Newline
Python Matrices and NumPy Arrays
Python String rsplit()
Python Program to Measure the Elapsed Time in Python
Python sorted()
Python Program to Convert Celsius To Fahrenheit
Python max()
Python List Comprehension
Python hex()
Python Program to Split a List Into Evenly Sized Chunks
Python next()
Python String find()
Python String capitalize()
Python staticmethod()
Python String isupper()
Python Tuple count()
Python Program to Convert Kilometers to Miles
Python Program to Check If Two Strings are Anagram
Python Dictionary pop()
Deep Learning in Python - LazyProgrammer
Python String isdigit()
Python time Module
Python String rindex()
Python Program to Convert String to Datetime