Fundamentals 12 min read

9 Powerful Python Techniques to Copy Files Efficiently

This tutorial explores nine different Python approaches—including shutil, os, threading, and subprocess—to copy files, explaining their behavior, performance implications, error handling, and platform considerations so you can choose the most suitable method for your application.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
9 Powerful Python Techniques to Copy Files Efficiently

Python provides many ready‑to‑use modules such as os, subprocess and shutil for file I/O operations. Understanding the most appropriate file‑copy technique is important because file I/O is performance‑intensive and can become a bottleneck. Depending on whether you need blocking or asynchronous copying, thread‑based execution, or cross‑platform compatibility, you should select the method that best fits your program design.

The nine Python file‑copy methods are:

shutil copyfile() shutil copy() shutil copyfileobj() shutil copy2() os popen() os system() threading Thread() (asynchronous copy)

subprocess call() subprocess

check_output()

shutil copyfile()

This method copies the source file’s contents to the destination file only if the destination is writable; otherwise it raises an IOError. It opens the source for reading, ignores file type, and uses copyfileobj() internally. An optional third argument lets you set the buffer size; by default the whole file is read at once. copyfile(source_file, destination_file) Key points:

Copies source content to the target file.

Raises IOError if the target is not writable.

Raises SameFileError if source and destination are the same.

Overwrites the target if it already exists with a different name.

Fails with Error 13 when the target is a directory.

Does not support special files such as character devices or pipes.

# Python Copy File - Sample Code
from shutil import copyfile
from sys import exit

source = input("Enter source file with full path: ")
target = input("Enter target file with full path: ")

# adding exception handling
try:
    copyfile(source, target)
except IOError as e:
    print("Unable to copy file. %s" % e)
    exit(1)
except:
    print("Unexpected error:", sys.exc_info())
    exit(1)

print("
File copy done!
")

while True:
    print("Do you like to print the file ? (y/n): ")
    check = input()
    if check == 'n':
        break
    elif check == 'y':
        file = open(target, "r")
        print("
Here follows the file content:
")
        print(file.read())
        file.close()
        print()
        break
    else:
        continue

shutil copy()

The copy() function behaves like the Unix cp command. If the destination is a directory, it creates a new file inside that directory with the same base name as the source and synchronizes the destination’s permission bits with the source.

shutil.copy(source_file, destination_path_or_dir)

shutil copyfileobj()

This method copies data from a source file object to a destination file object. An optional buffer size argument (default 16 KB) controls how many bytes are read into memory at a time. If the destination is a file object, you must close it after copying.

from shutil import copyfileobj
status = False
if isinstance(target, string_types):
    target = open(target, 'wb')
    status = True
try:
    copyfileobj(self.stream, target, buffer_size)
finally:
    if status:
        target.close()

shutil copy2()

copy2()

works like copy() but also preserves metadata such as access and modification timestamps. It raises SameFileError when the source and destination refer to the same file.

os popen()

os.popen()

opens a pipe to or from a command. The optional mode argument can be 'r' (default) or 'w'. The optional bufsize controls buffering: 0 for unbuffered, 1 for line‑buffered, >1 for a specific buffer size, and a negative value lets the system choose the default. os.popen(command[, mode[, bufsize]]) Example for Windows:

import os
os.popen('copy 1.txt.py 2.txt.py')

Example for Linux:

import os
os.popen('cp 1.txt.py 2.txt.py')

os system()

os.system()

runs a system command in a subshell and returns the command’s exit status. It is a simple way to execute external commands on both Windows and Linux.

# Windows example
import os
os.system('copy 1.txt.py 2.txt.py')

# Linux example
import os
os.system('cp 1.txt.py 2.txt.py')

Asynchronous copy with threading.Thread

To copy files without blocking the main thread, you can start a Thread that calls shutil.copy(). Remember to use proper locking if multiple threads access the same files.

import shutil
from threading import Thread

src = "1.txt.py"
dst = "3.txt.py"
Thread(target=shutil.copy, args=[src, dst]).start()

subprocess call()

The subprocess.call() function runs a command in a new process and returns its exit status. It replaces older functions like os.system and os.popen. You can check the status to detect failures.

import subprocess
src = "1.txt.py"
dst = "2.txt.py"
cmd = 'copy "%s" "%s"' % (src, dst)
status = subprocess.call(cmd, shell=True)
if status != 0:
    if status < 0:
        print("Killed by signal", status)
    else:
        print("Command failed with return code - ", status)
else:
    print('Execution of %s passed!' % cmd)

subprocess check_output()

subprocess.check_output()

runs a command and captures its standard output. It raises an exception if the command returns a non‑zero exit code, making it useful for error handling while still retrieving command output.

import os, subprocess
src = os.path.realpath(os.getcwd() + "http://cdn.techbeamers.com/1.txt.py")
dst = os.path.realpath(os.getcwd() + "http://cdn.techbeamers.com/2.txt.py")
cmd = 'copy "%s" "%s"' % (src, dst)
status = subprocess.check_output(['copy', src, dst], shell=True)
print("status: ", status.decode('utf-8'))
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

osshutilsubprocessfile copy
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.