How to create a duplicate file of an existing file using Python?
In this article, we will discuss how to create a duplicate of the existing file in Python. Below are the source and destination folders, before creating the duplicate file in the destination folder.
After a duplicate file has been created in the destination folder, it looks like the image below.
For automating of copying and removal of files in Python, shutil module is used. It offers a number of high-level operations on files and collections of files. Using shutil module, we can copy files as well as an entire directory.
It copies the contents of the source file to the destination file in the most efficient way possible. It does not use file objects and also does not copy metadata and permissions.
Syntax : shutil.copyfile(src, dst, *, follow_symlinks=True)
Parameters:
- src – src here is the complete path of the source file.
- dest – dest is the complete path of the destination file or directory.The destination location must be writable.
- follow_symlinks (optional) – The default value of this parameter is True. If it is set to False and src is a symbolic link, a new symbolic link will be created instead of copying the file src points to.
Return Type:- It returns the path of the newly created duplicate file.
Code:
Python3
import os
D = r "F:\Dest"
import shutil
print ( "Before copying file:" )
print (os.listdir(D))
src = r "C:\Users\YASH\OneDrive\Desktop\small\Src\Test.py"
dest = r "F:\Dest\Test.py"
path = shutil.copyfile(src,dest)
print ( "After copying file:" )
print (os.listdir(D))
print ( "Path of the duplicate file is:" )
print (path)
|
Output:
Before copying the file:
['in.txt', 'out.txt']
After copying the file:
['in.txt', 'out.txt', 'Test.py']
Path of the duplicate file is:
F:\Dest\Test.py
It also copies the contents of the source file to the destination file or directory. Unlike copyfile(), shutil.copy() also copies the permissions of the source file.
Syntax : shutil.copy(src, dst, *, follow_symlinks=True)
Parameters:-
- src – src here is the complete path of the source file.
- dest – dest is the complete path of the destination file or directory.The destination location must be writable.
- follow_symlinks (optional) – The default value of this parameter is True. If it is set to False and src is a symbolic link, a new symbolic link will be created instead of copying the file src points to.
Return Type:- It returns the path of the newly created duplicate file.
Code:
Python3
import os, stat
D = r "F:\Dest"
import shutil
print ( "Before copying file:" )
print (os.listdir(D))
src = r "C:\Users\YASH\OneDrive\Desktop\small\Src\Test.py"
os.chmod(src, stat.S_IRWXO)
dest = r "F:\Dest\Test.py"
path = shutil.copy(src,dest)
print (os.access(path, stat.S_IRWXO))
print ( "After copying file:" )
print (os.listdir(D))
print ( "Path of the duplicate file is:" )
print (path)
|
Output:
Before copying the file:
['in.txt', 'out.txt']
After copying the file:
False
['in.txt', 'out.txt', 'Test.py']
Path of the duplicate file is:
F:\Dest\Test.py
It is almost similar to shutil.copy(), except copy2() also attempts to preserve metadata. When follow_symlinks is set to False, and src is a symbolic link, copy2() attempts to copy all metadata from the src symbolic link to the newly-created dst symbolic link.
Syntax : shutil.copy2(src, dst, *, follow_symlinks=True)
Parameters:
- src – src here is the complete path of the source file.
- dest – dest is the complete path of the destination file or directory.The destination location must be writable.
- follow_symlinks (optional) – The default value of this parameter is True. If it is set to False and src is a symbolic link, a new symbolic link will be created instead of copying the file src points to.
Return Type:- It returns the path of the newly created duplicate file.
Code:
Python3
import os
D = r "F:\Dest"
import shutil
print ( "Before copying file:" )
print (os.listdir(D))
src = r "C:\Users\YASH\OneDrive\Desktop\small\Src\Test.py"
dest = r "F:\Dest\Test.py"
path = shutil.copy2(src,dest)
print ( "After copying file:" )
print (os.listdir(D))
print ( "Path of the duplicate file is:" )
print (path)
|
Output:
Before copying the file:
['in.txt', 'out.txt']
After copying the file:
['in.txt', 'out.txt', 'Test.py']
Path of the duplicate file is:
F:\Dest\Test.py
Last Updated :
26 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...