---Advertisement---

Python Copy File Methods: How to Copy Files with shutil.copy() and shutil.copystat() Great in 2025

By Bhavani

Updated On:

---Advertisement---
 Python Copy File Methods

๐Ÿ‘‰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

MethodDescription
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.

Python Check if File Exists

Download Python

Leave a Comment

Index