Explain Codes LogoExplain Codes Logo

How to copy files

python
file-operations
shutil
pathlib
Nikita BarsukovbyNikita Barsukov·Sep 16, 2024
TLDR

In Python, you have 3 simple choices for copying files:

  1. shutil.copy(src, dst): Use when you just need to duplicate the file contents.
  2. shutil.copy2(src, dst): Use when you need to keep the original file dates and metadata.
  3. shutil.copytree(src, dst): Opt for this when you need to copy an entire directory.
import shutil # Your basic file copy: shutil.copy('src.txt', 'dest.txt') # Just the basics, ma'am. # File copy with metadata: shutil.copy2('src.txt', 'dest.txt') # Because some like it hot with metadata.

Choose your function superhero depending on whether saving the file metadata is part of your data rescue mission.

Before you press the "Copy" button...

Always remember, every action has its reaction while dealing with file copies. Here are the caped crusaders and villains you need to watch out for:

Permissions and Metadata

Even Superman faces kryptonite. Your kryptonite could be permissions and metadata here. shutil.copy2 preserves the original file's metadata but ensure your user accesses are in place while handling file copies.

Overwriting Files

Remember when Thanos snapped and half the universe disappeared? Similarly, existing destination files will be overwritten without a whisper. Don't let your files face Thanos's snap.

Special files and devices

Copy files, they said. It's easy, they said. Except when it's a special file like a device or OS file. shutil will politely reject with an IOError.

System commands and shell scripts

Sometimes, some files demand the red carpet treatment. shutil may not cut it. In such scenarios, use os.system or subprocess.call for executing system commands for file operations.

When you feel the need... the need for speed!

Python is your Top Gun for more advanced file operations. Let's explore!

Working with open file objects

Riding a bike is fun. But you can't ignore the traffic rules. Handle open file descriptors with shutil.copyfileobj. Offers low-level control over file data streaming like a boss!

In the Matrix, you may want to follow the white rabbit, or you may not. Control copying of symlinks with shutil.copy and shutil.copy2 with the follow_symlinks argument.

Roger that, Command Control!

For platform-specific commands, os and subprocess provide your emergency exit to fuel your copying operations.

Other Notable Highlights

Pathlib for the win!

Tired of mixed-up file paths? Use pathlib.Path for dealing with paths. It's like using an ABC book for dealing with paths.

from pathlib import Path src = Path('/path/to/src.txt') dst = Path('/path/to/dest.txt') # Copy with metadata: shutil.copy2(src, dst) # We love the metadata, don't we?

Smile, you're on exception camera!

Planning for mishaps gives you control. Handle exceptions for smoother operations.

try: shutil.copy(src, dst) # Brace for impact! except IOError as e: print(f"Sorry, cannot copy file. {e}") except: print("Oops, something went wrong!", sys.exc_info())

Speed matters!

Because even in copying, Size Matters! For large files or directories, use buffered I/O for speed. Your operations will say Thank You!