Copying the Metadata along with File
shutil.copy2() method in Python is used to copy the content of the source file to the destination file or directory. This method is identical to shutil.copy() method but it also tries to preserve the file’s metadata.
Syntax: shutil.copy2(source, destination, *, follow_symlinks = True)
Parameter:
- source: A string representing the path of the source file.
- destination: A string representing the path of the destination file or directory.
- follow_symlinks (optional) : The default value of this parameter is True. If it is False and source represents a symbolic link then it attempts to copy all metadata from the source symbolic link to the newly-created destination symbolic link. This functionality is platform dependent.
Return Type: This method returns a string which represents the path of newly created file.
Python3
# Python program to explain shutil.copy2() method # importing os module import os # importing shutil module import shutil # path path = 'csv/' # List files and directories # in '/home/User/Documents' print ( "Before copying file:" ) print (os.listdir(path)) # Source path source = "csv/main.py" # Print the metadeta # of source file metadata = os.stat(source) print ( "Metadata:" , metadata, "\n" ) # Destination path destination = "csv/gfg/check.txt" # Copy the content of # source to destination dest = shutil.copy2(source, destination) # List files and directories # in "/home / User / Documents" print ( "After copying file:" ) print (os.listdir(path)) # Print the metadata # of the destination file matadata = os.stat(destination) print ( "Metadata:" , metadata) # Print path of newly # created file print ( "Destination path:" , dest) |
Output:
Before copying file:
[‘archive (2)’, ‘c.jpg’, ‘c.PNG’, ‘Capture.PNG’, ‘cc.jpg’, ‘check.zip’, ‘cv.csv’, ‘d.png’, ‘Done! Terms And Conditions Generator – The Fastest Free Terms and Conditions Generator!.pdf’, ‘file1.csv’, ‘gfg’, ‘haarcascade_frontalface_alt2.xml’, ‘log_transformed.jpg’, ‘main.py’, ‘nba.csv’, ‘new_gfg.png’, ‘r.gif’, ‘Result -_ Terms and Conditions are Ready!.pdf’, ‘rockyou.txt’, ‘sample.txt’]
Metadata: os.stat_result(st_mode=33206, st_ino=2251799814202896, st_dev=1689971230, st_nlink=1, st_uid=0, st_gid=0, st_size=1916, st_atime=1612953710, st_mtime=1612613202, st_ctime=1612522940)
After copying file:
[‘archive (2)’, ‘c.jpg’, ‘c.PNG’, ‘Capture.PNG’, ‘cc.jpg’, ‘check.zip’, ‘cv.csv’, ‘d.png’, ‘Done! Terms And Conditions Generator – The Fastest Free Terms and Conditions Generator!.pdf’, ‘file1.csv’, ‘gfg’, ‘haarcascade_frontalface_alt2.xml’, ‘log_transformed.jpg’, ‘main.py’, ‘nba.csv’, ‘new_gfg.png’, ‘r.gif’, ‘Result -_ Terms and Conditions are Ready!.pdf’, ‘rockyou.txt’, ‘sample.txt’]
Metadata: os.stat_result(st_mode=33206, st_ino=2251799814202896, st_dev=1689971230, st_nlink=1, st_uid=0, st_gid=0, st_size=1916, st_atime=1612953710, st_mtime=1612613202, st_ctime=1612522940)
Destination path: csv/gfg/check.txt
Example 2: If the destination is a directory
Python3
# Python program to explain shutil.copy2() method # importing os module import os # importing shutil module import shutil # Source path source = "csv/main.py" # Destination path destination = "csv/gfg/" # Copy the content of # source to destination dest = shutil.copy2(source, destination) # List files and directories # in "/home / User / Desktop" print ( "After copying file:" ) print (os.listdir(destination)) # Print path of newly # created file print ( "Destination path:" , dest) |
Output:
After copying file:
[‘cc.jpg’, ‘check.txt’, ‘log_transformed.jpg’, ‘main.py’, ‘main2.py’]
Destination path: csv/gfg/main.py
Shutil Module in Python
Shutil module offers high-level operation on a file like a copy, create, and remote operation on the file. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories. In this article, we will learn this module.