
๐Python Copy File Methods
Python Copy File Methods: Python offers efficient built-in methods for copying files and their metadata using the shutil module. These methods simplify file duplication, preserving essential information like permissions and modification times.
In this guide, you’ll learn:
โ
How to use shutil.copy() to copy files
โ
How to use shutil.copystat() to copy file metadata
โ
Step-by-step code examples for better understanding
๐1. Copy Files Using shutil.copy()
Python Copy File Methods: The shutil.copy() method is used to copy a file’s content to a new file. However, it does not copy metadata like permissions or modification times.
๐Python Copy File Methods Example Code:
python
import os
import shutil
from os import path
def main():
# Check if the file exists
if path.exists(“Software_Moji_Moji.txt”):
# Get the file path
src = path.realpath(“Software_Moji_Moji.txt”)
# Separate the path and file name
head, tail = path.split(src)
print(“Path:”, head)
print(“File:”, tail)
# Create a backup copy with ‘.bak’ extension
dst = src + “.bak”
shutil.copy(src, dst)
print(“File copied successfully!”)
if __name__ == “__main__”:
main()
๐Output:
mathematica
Path: /user/files
File: Software_Moji_Moji.txt
File copied successfully!
๐2. Copy Files with Metadata Using shutil.copystat()
The shutil.copy() method only copies content, not metadata. To copy permissions, timestamps, and other metadata, use shutil.copystat().
๐Example Code:
python
import os
import shutil
from os import path
def main():
if path.exists(“Software_Moji_Moji.txt”):
src = path.realpath(“Software_Moji_Moji.txt”)
dst = src + “.bak”
# Copy the file
shutil.copy(src, dst)
# Copy metadata (permissions, modification time, etc.)
shutil.copystat(src, dst)
print(“File and metadata copied successfully!”)
if __name__ == “__main__”:
main()
๐Output:
arduino
File and metadata copied successfully!
๐3. Fetch File Modification Details
To retrieve the last modification time of a file, you can use the os.path.getmtime() method.
๐Example Code:
python
import os
from os import path
import datetime
import time
def main():
# Get file modification time
t = time.ctime(path.getmtime(“Software_Moji_Moji.txt.bak”))
print(“Last modified (Format 1):”, t)
# Alternative format for modification time
print(“Last modified (Format 2):”, datetime.datetime.fromtimestamp(path.getmtime(“Software_Moji_Moji.txt.bak”)))
if __name__ == “__main__”:
main()
๐Output:
mathematica
Last modified (Format 1): Mon Jan 8 13:35:51 2018
Last modified (Format 2): 2018-01-08 13:35:51.334072
๐4. Key Differences Between Methods
Method | Description |
shutil.copy() | Copies file content only (no metadata). |
shutil.copystat() | Copies file metadata like permissions, modification time, etc. |
os.path.getmtime() | Fetches the last modification time of a file. |
๐5. Summary
โ
Use shutil.copy() to copy files efficiently.
โ
Use shutil.copystat() to copy metadata like permissions and timestamps.
โ
For fetching modification details, use os.path.getmtime() for accurate file information.