Copying Files (shutil.copy vs copy2)

๐Ÿท๏ธ Operating System and System Operations / The shutil Module

When working with files in Python, you'll often need to duplicate them. The shutil module provides two main functions for copying files: shutil.copy() and shutil.copy2(). Both copy a file from one location to another, but they differ in what metadata they preserve. Understanding this difference helps you choose the right tool for your automation and backup tasks.


โš™๏ธ What Is shutil.copy()?

shutil.copy() copies the contents of a source file to a destination. It preserves the file's permission bits (read, write, execute) but does not preserve other metadata like timestamps.

  • Copies file content and permission mode
  • Does not preserve creation or modification timestamps
  • Returns the path to the newly created file
  • Best for quick file duplication where timestamps don't matter

Example usage:
shutil.copy("source.txt", "destination.txt") copies the content and permissions from source.txt to destination.txt. The destination file will have the current system time as its timestamp.


๐Ÿ› ๏ธ What Is shutil.copy2()?

shutil.copy2() is identical to shutil.copy() in terms of copying file content and permissions, but it goes one step further โ€” it also attempts to preserve as much metadata as possible.

  • Copies file content and permission mode
  • Preserves creation time, modification time, and access time
  • Returns the path to the newly created file
  • Ideal for backups and archival operations where original timestamps are important

Example usage:
shutil.copy2("source.txt", "destination.txt") copies everything from source.txt to destination.txt, including the original file's timestamps.


๐Ÿ“Š Comparison Table: shutil.copy vs shutil.copy2

Feature shutil.copy() shutil.copy2()
File content โœ… Copied โœ… Copied
Permission bits โœ… Preserved โœ… Preserved
Modification time โŒ Not preserved โœ… Preserved
Creation time โŒ Not preserved โœ… Preserved
Access time โŒ Not preserved โœ… Preserved
Use case Quick copies, temporary files Backups, archives, file synchronization

๐Ÿ•ต๏ธ When to Use Each Function

Use shutil.copy() when: - You only need the file content and basic permissions - You are creating temporary working copies - Timestamps are irrelevant to your workflow - You want slightly faster execution

Use shutil.copy2() when: - You are creating backups and want to preserve original file dates - You are synchronizing files between directories - You need to maintain accurate file history - The metadata matters for auditing or compliance


๐Ÿงช Practical Example Scenario

Imagine you have a configuration file named config.ini that was last modified on January 15th.

  • Using shutil.copy("config.ini", "backup/config.ini") creates a copy with today's date as the modification time.
  • Using shutil.copy2("config.ini", "backup/config.ini") creates a copy that still shows January 15th as the modification time.

For most engineers, shutil.copy2() is the safer default choice because it preserves more information. You can always fall back to shutil.copy() when speed is critical and metadata doesn't matter.


โœ… Summary

Both shutil.copy() and shutil.copy2() are essential tools for file operations in Python. The key takeaway is simple: if you care about keeping the original file's timestamps, use copy2. If you just need the file content and permissions, use copy. Choose based on your specific need, and you'll handle file copying like a pro.


The shutil.copy and shutil.copy2 functions copy files from one location to another, with copy2 also preserving file metadata like timestamps.


๐Ÿ“„ Example 1: Basic file copy with shutil.copy

This example copies a simple text file to a new location.

import shutil

shutil.copy("source.txt", "destination.txt")

๐Ÿ“ค Output: 'destination.txt'


๐Ÿ“„ Example 2: Copy file to a different directory

This example copies a file into an existing folder.

import shutil

shutil.copy("report.pdf", "backups/")

๐Ÿ“ค Output: 'backups/report.pdf'


๐Ÿ“„ Example 3: Copy file with full metadata using copy2

This example preserves the original file's modification and access times.

import shutil
import os
import time

shutil.copy2("config.ini", "config_backup.ini")

original_time = os.path.getmtime("config.ini")
copy_time = os.path.getmtime("config_backup.ini")

print(original_time == copy_time)

๐Ÿ“ค Output: True


๐Ÿ“„ Example 4: Compare timestamps between copy and copy2

This example shows that shutil.copy does not preserve the original timestamp.

import shutil
import os
import time

shutil.copy("data.csv", "data_copy.csv")
shutil.copy2("data.csv", "data_copy2.csv")

original_mtime = os.path.getmtime("data.csv")
copy_mtime = os.path.getmtime("data_copy.csv")
copy2_mtime = os.path.getmtime("data_copy2.csv")

print("Original mtime:", original_mtime)
print("copy mtime:", copy_mtime)
print("copy2 mtime:", copy2_mtime)
print("copy matches original:", original_mtime == copy_mtime)
print("copy2 matches original:", original_mtime == copy2_mtime)

๐Ÿ“ค Output: Original mtime: 1698001234.5678901
๐Ÿ“ค Output: copy mtime: 1698005678.9012345
๐Ÿ“ค Output: copy2 mtime: 1698001234.5678901
๐Ÿ“ค Output: copy matches original: False
๐Ÿ“ค Output: copy2 matches original: True


๐Ÿ“„ Example 5: Copy with error handling for missing source

This example safely attempts to copy a file that may not exist.

import shutil
import os

source_file = "missing_file.txt"
destination_file = "backup_missing.txt"

if os.path.exists(source_file):
    shutil.copy2(source_file, destination_file)
    print("File copied successfully")
else:
    print(f"Source file '{source_file}' not found")

๐Ÿ“ค Output: Source file 'missing_file.txt' not found


Comparison Table: shutil.copy vs shutil.copy2

Feature shutil.copy shutil.copy2
Copies file contents โœ… Yes โœ… Yes
Copies file permissions โœ… Yes โœ… Yes
Preserves timestamps (mtime, atime) โŒ No โœ… Yes
Preserves other metadata โŒ No โœ… Yes (as much as possible)
Use case Quick copies without history Backups needing original timestamps

When working with files in Python, you'll often need to duplicate them. The shutil module provides two main functions for copying files: shutil.copy() and shutil.copy2(). Both copy a file from one location to another, but they differ in what metadata they preserve. Understanding this difference helps you choose the right tool for your automation and backup tasks.


โš™๏ธ What Is shutil.copy()?

shutil.copy() copies the contents of a source file to a destination. It preserves the file's permission bits (read, write, execute) but does not preserve other metadata like timestamps.

  • Copies file content and permission mode
  • Does not preserve creation or modification timestamps
  • Returns the path to the newly created file
  • Best for quick file duplication where timestamps don't matter

Example usage:
shutil.copy("source.txt", "destination.txt") copies the content and permissions from source.txt to destination.txt. The destination file will have the current system time as its timestamp.


๐Ÿ› ๏ธ What Is shutil.copy2()?

shutil.copy2() is identical to shutil.copy() in terms of copying file content and permissions, but it goes one step further โ€” it also attempts to preserve as much metadata as possible.

  • Copies file content and permission mode
  • Preserves creation time, modification time, and access time
  • Returns the path to the newly created file
  • Ideal for backups and archival operations where original timestamps are important

Example usage:
shutil.copy2("source.txt", "destination.txt") copies everything from source.txt to destination.txt, including the original file's timestamps.


๐Ÿ“Š Comparison Table: shutil.copy vs shutil.copy2

Feature shutil.copy() shutil.copy2()
File content โœ… Copied โœ… Copied
Permission bits โœ… Preserved โœ… Preserved
Modification time โŒ Not preserved โœ… Preserved
Creation time โŒ Not preserved โœ… Preserved
Access time โŒ Not preserved โœ… Preserved
Use case Quick copies, temporary files Backups, archives, file synchronization

๐Ÿ•ต๏ธ When to Use Each Function

Use shutil.copy() when: - You only need the file content and basic permissions - You are creating temporary working copies - Timestamps are irrelevant to your workflow - You want slightly faster execution

Use shutil.copy2() when: - You are creating backups and want to preserve original file dates - You are synchronizing files between directories - You need to maintain accurate file history - The metadata matters for auditing or compliance


๐Ÿงช Practical Example Scenario

Imagine you have a configuration file named config.ini that was last modified on January 15th.

  • Using shutil.copy("config.ini", "backup/config.ini") creates a copy with today's date as the modification time.
  • Using shutil.copy2("config.ini", "backup/config.ini") creates a copy that still shows January 15th as the modification time.

For most engineers, shutil.copy2() is the safer default choice because it preserves more information. You can always fall back to shutil.copy() when speed is critical and metadata doesn't matter.


โœ… Summary

Both shutil.copy() and shutil.copy2() are essential tools for file operations in Python. The key takeaway is simple: if you care about keeping the original file's timestamps, use copy2. If you just need the file content and permissions, use copy. Choose based on your specific need, and you'll handle file copying like a pro.

Interactive Views

You are currently in ๐Ÿ“š All-in-One mode. Use the tabs at the top to switch to ๐Ÿ“– Theory Only or ๐Ÿ’ป Code Only views.

The shutil.copy and shutil.copy2 functions copy files from one location to another, with copy2 also preserving file metadata like timestamps.


๐Ÿ“„ Example 1: Basic file copy with shutil.copy

This example copies a simple text file to a new location.

import shutil

shutil.copy("source.txt", "destination.txt")

๐Ÿ“ค Output: 'destination.txt'


๐Ÿ“„ Example 2: Copy file to a different directory

This example copies a file into an existing folder.

import shutil

shutil.copy("report.pdf", "backups/")

๐Ÿ“ค Output: 'backups/report.pdf'


๐Ÿ“„ Example 3: Copy file with full metadata using copy2

This example preserves the original file's modification and access times.

import shutil
import os
import time

shutil.copy2("config.ini", "config_backup.ini")

original_time = os.path.getmtime("config.ini")
copy_time = os.path.getmtime("config_backup.ini")

print(original_time == copy_time)

๐Ÿ“ค Output: True


๐Ÿ“„ Example 4: Compare timestamps between copy and copy2

This example shows that shutil.copy does not preserve the original timestamp.

import shutil
import os
import time

shutil.copy("data.csv", "data_copy.csv")
shutil.copy2("data.csv", "data_copy2.csv")

original_mtime = os.path.getmtime("data.csv")
copy_mtime = os.path.getmtime("data_copy.csv")
copy2_mtime = os.path.getmtime("data_copy2.csv")

print("Original mtime:", original_mtime)
print("copy mtime:", copy_mtime)
print("copy2 mtime:", copy2_mtime)
print("copy matches original:", original_mtime == copy_mtime)
print("copy2 matches original:", original_mtime == copy2_mtime)

๐Ÿ“ค Output: Original mtime: 1698001234.5678901
๐Ÿ“ค Output: copy mtime: 1698005678.9012345
๐Ÿ“ค Output: copy2 mtime: 1698001234.5678901
๐Ÿ“ค Output: copy matches original: False
๐Ÿ“ค Output: copy2 matches original: True


๐Ÿ“„ Example 5: Copy with error handling for missing source

This example safely attempts to copy a file that may not exist.

import shutil
import os

source_file = "missing_file.txt"
destination_file = "backup_missing.txt"

if os.path.exists(source_file):
    shutil.copy2(source_file, destination_file)
    print("File copied successfully")
else:
    print(f"Source file '{source_file}' not found")

๐Ÿ“ค Output: Source file 'missing_file.txt' not found


Comparison Table: shutil.copy vs shutil.copy2

Feature shutil.copy shutil.copy2
Copies file contents โœ… Yes โœ… Yes
Copies file permissions โœ… Yes โœ… Yes
Preserves timestamps (mtime, atime) โŒ No โœ… Yes
Preserves other metadata โŒ No โœ… Yes (as much as possible)
Use case Quick copies without history Backups needing original timestamps