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 int()
Python Program to Find ASCII Value of Character
Python Program to Print the Fibonacci sequence
Python String zfill()
Java Program to Implement the Program Used in grep/egrep/fgrep
Python String translate()
Python round()
Python Program to Slice Lists
Python String expandtabs()
Python Objects and Classes
Python Program to Iterate Through Two Lists in Parallel
Python divmod()
Python List sort()
Python Generators
Python Program to Find the Size (Resolution) of a Image
Python Get Current time
Python Program to Capitalize the First Character of a String
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Sort a Dictionary by Value
Python Program to Convert Bytes to a String
Python Program to Illustrate Different Set Operations
Python Set pop()
Python List insert()
Python Set issubset()
Python Program to Find Armstrong Number in an Interval
Python dir()
Python tuple()
Python Input, Output and Import
Python String startswith()
Python String isdigit()
Python List