How to Copy Files to Another Directory in Python

A wedding photographer in Austin asked me to copy files to another directory in Python after every shoot: the JPEGs from her camera folder go into the client’s delivery folder, and an edited photo never gets overwritten.

The answer is shutil.copy2(src, dest_folder) inside a loop, with an exists() check before each copy. It copies the file into the folder, keeps the name and keeps the shoot date.

In this tutorial, I’ll build that script step by step, and you’ll learn how to copy files to another directory in Python: one file, a filtered set, a whole folder tree, and the new Python 3.14 Path.copy_into().

Here’s what we’re building. The first run copies four photos; the second run copies nothing, because they’re already there:

VS Code output of a Python script copying four JPG photos into a delivery folder and skipping all four on the second run
First run: 4 copied, 0 skipped. Second run: 0 copied, 4 skipped, so nothing gets overwritten.

Every result below comes from real runs on Python 3.12.5 on Windows 11 (plus Python 3.14.8 for the pathlib section), shown in VS Code.

Before You Start: the sample files to copy

You need Python 3.8 or newer for dirs_exist_ok and 3.14 for the pathlib methods. The shutil module ships with Python, so there’s nothing to install. If you’re not sure the folders exist yet, check if a directory exists in Python first.

Our running sample is a folder called camera, right next to the script. Here’s what’s inside it:

ItemTypeNotes
IMG_2041.jpg to IMG_2044.jpgJPEG photosThe files the client gets; shot on 2026-10-24
clip_0007.movVideoNot part of the delivery
shot_list.txtTextCeremony and portrait times
Thumbs.dbWindows cacheJunk we never want to copy
raw/IMG_2041.CR3, raw/IMG_2042.CR3SubfolderRaw files for the backup only

This short script prints the folder so you can confirm your copy matches mine:

from pathlib import Path

for item in sorted(Path("camera").rglob("*")):
    size = "folder" if item.is_dir() else f"{item.stat().st_size:,} bytes"
    print(f"{item.as_posix():<24} {size}")

Output:

camera/clip_0007.mov     92,160 bytes
camera/IMG_2041.jpg      48,213 bytes
camera/IMG_2042.jpg      51,907 bytes
camera/IMG_2043.jpg      46,380 bytes
camera/IMG_2044.jpg      53,122 bytes
camera/raw               folder
camera/raw/IMG_2041.CR3  81,920 bytes
camera/raw/IMG_2042.CR3  84,992 bytes
camera/shot_list.txt     76 bytes
camera/Thumbs.db         3,072 bytes

That’s 9 files, two of them inside the raw subfolder. To list a folder like this in other ways, see how to list files in a directory with Python.

Step 1: Copy one file to another directory with shutil.copy()

The goal of this step is the smallest possible copy: one photo into a folder called delivery.

  1. Create a file named copy_one.py next to the camera folder.
  2. Paste the code below and save it.
  3. Run it with python copy_one.py in the VS Code terminal.
import shutil
from pathlib import Path

dest = Path("delivery")
dest.mkdir(exist_ok=True)

result = shutil.copy("camera/IMG_2041.jpg", dest)
print("copied to:", result)
print("delivery now has:", [p.name for p in dest.iterdir()])

Output:

copied to: delivery\IMG_2041.jpg
delivery now has: ['IMG_2041.jpg']
VS Code output of Python shutil.copy copying IMG_2041.jpg into the delivery folder and returning the new path
shutil.copy() returns the path of the new file, here delivery\IMG_2041.jpg.

When the second argument is a folder, shutil.copy() keeps the original file name. It returns the new path, which is handy for logging.

mkdir(exist_ok=True) matters here (without it, a first run on a fresh machine fails because delivery doesn’t exist).

Checkpoint: delivery now holds exactly one file, IMG_2041.jpg.

Need the copy under a different name? Pass a full file path instead of a folder; my guide on copying a file and renaming it in Python covers that case.

Step 2: Copy all JPG files to another directory with glob

Same camera folder, but now we want every photo and nothing else. Path.glob("*.jpg") picks the matching files, and the loop copies each one:

import shutil
from pathlib import Path

src, dest = Path("camera"), Path("delivery")
dest.mkdir(exist_ok=True)

for photo in sorted(src.glob("*.jpg")):
    shutil.copy(photo, dest)
    print("copied", photo.name)

print(len(list(dest.iterdir())), "files in", dest)

Output:

copied IMG_2041.jpg
copied IMG_2042.jpg
copied IMG_2043.jpg
copied IMG_2044.jpg
4 files in delivery
VS Code output of a Python loop using Path.glob to copy four JPG files from camera to delivery
Only the four .jpg files are copied; the video, text file, cache and raw folder stay behind.

sorted() isn’t required, but it makes the order predictable, so the log reads IMG_2041 to IMG_2044.

Checkpoint: delivery holds 4 files, and clip_0007.mov isn’t one of them.

If you prefer the older os style, os.listdir() plus os.path.isfile() copies every file in the top level while skipping subfolders:

import os
import shutil

src, dest = "camera", "delivery"
os.makedirs(dest, exist_ok=True)

for name in os.listdir(src):
    path = os.path.join(src, name)
    if os.path.isfile(path):          # skips the raw/ folder
        shutil.copy(path, dest)

print(sorted(os.listdir(dest)))

Output:

['IMG_2041.jpg', 'IMG_2042.jpg', 'IMG_2043.jpg', 'IMG_2044.jpg', 'Thumbs.db', 'clip_0007.mov', 'shot_list.txt']

That copies all 7 top-level files, including Thumbs.db, which is why a pattern is usually better. Building paths with os.path.join in Python keeps this version working on Windows, macOS and Linux.

Step 3: Keep the original date with shutil.copy2()

Photographers sort by date, so the copies should keep the shoot date. Here’s what each function does to the modified time of IMG_2042.jpg:

import shutil
import time
from pathlib import Path

src = Path("camera/IMG_2042.jpg")
dest = Path("delivery")
dest.mkdir(exist_ok=True)

a = shutil.copy(src, dest / "with_copy.jpg")
b = shutil.copy2(src, dest / "with_copy2.jpg")

def day(p):
    return time.strftime("%Y-%m-%d", time.localtime(Path(p).stat().st_mtime))

for p in (src, a, b):
    print(f"{Path(p).name:<15} modified {day(p)}")

Output:

IMG_2042.jpg    modified 2026-10-24
with_copy.jpg   modified 2026-10-31
with_copy2.jpg  modified 2026-10-24
VS Code output comparing the modified date after Python shutil.copy and shutil.copy2 on the same photo
copy() stamps the copy with today’s date; copy2() keeps the source’s 2026-10-24 date.

shutil.copy() copies the content and the permission bits. shutil.copy2() also copies the timestamps and other metadata the system allows.

Checkpoint: with_copy2.jpg shows 2026-10-24, the same day as the source.

Note: neither function copies the owner or the Windows security settings (ACLs). That’s documented, not a bug.

Step 4: Stop shutil.copy() from overwriting existing files

This is the step that protects the client’s edits. Say the client already retouched IMG_2041.jpg in the delivery folder (we fake that with write_bytes()). Watch what a plain copy does:

import shutil
from pathlib import Path

dest = Path("delivery")
dest.mkdir(exist_ok=True)
edited = dest / "IMG_2041.jpg"
edited.write_bytes(b"retouched" * 9000)   # the client's edited copy

print("before:", f"{edited.stat().st_size:,}", "bytes")
shutil.copy("camera/IMG_2041.jpg", dest)  # no warning at all
print("after: ", f"{edited.stat().st_size:,}", "bytes")

print("--- safe version ---")
for photo in sorted(Path("camera").glob("IMG_204[12].jpg")):
    target = dest / photo.name
    if target.exists():
        print("skip", photo.name, "(already delivered)")
        continue
    shutil.copy2(photo, target)
    print("copy", photo.name)

Output:

before: 81,000 bytes
after:  48,213 bytes
--- safe version ---
skip IMG_2041.jpg (already delivered)
copy IMG_2042.jpg
VS Code output showing Python shutil.copy silently replacing an edited file, then a loop skipping files that already exist
The edited file goes from 81,000 to 48,213 bytes with no warning; the safe loop skips it.

shutil.copy() replaced the 81,000-byte edited file with the 48,213-byte original and said nothing. The safe loop checks target.exists() first, skips IMG_2041.jpg and copies only IMG_2042.jpg.

Checkpoint: the second half of the output says skip IMG_2041.jpg and copy IMG_2042.jpg.

The same exists-then-act idea shows up in checking if a file exists in Python, which compares exists(), is_file() and try/except.

Step 5: Copy a whole directory with shutil.copytree()

The photographer also wants a full backup of the camera folder, subfolders included, minus the Windows junk. That’s shutil.copytree():

import shutil
from pathlib import Path

backup = Path("backup") / "camera_2026-10-24"
skip = shutil.ignore_patterns("Thumbs.db", "*.tmp")

shutil.copytree("camera", backup, ignore=skip)
for item in sorted(backup.rglob("*")):
    print(" ", item.relative_to(backup).as_posix())

try:
    shutil.copytree("camera", backup)      # second run
except FileExistsError as err:
    print("FileExistsError:", err.strerror)

shutil.copytree("camera", backup, ignore=skip, dirs_exist_ok=True)
print("dirs_exist_ok=True: merged, no error")

Output:

  clip_0007.mov
  IMG_2041.jpg
  IMG_2042.jpg
  IMG_2043.jpg
  IMG_2044.jpg
  raw
  raw/IMG_2041.CR3
  raw/IMG_2042.CR3
  shot_list.txt
FileExistsError: Cannot create a file when that file already exists
dirs_exist_ok=True: merged, no error
VS Code output of Python shutil.copytree backing up the camera folder with ignore_patterns, then FileExistsError and dirs_exist_ok
The backup has every file and the raw subfolder but no Thumbs.db. A second plain run raises FileExistsError.

copytree() creates the destination, including missing parent folders, and copies everything inside it with copy2() by default. ignore_patterns() filters files by name at every level.

Running it again without dirs_exist_ok=True fails with FileExistsError. With it (Python 3.8+), the call merges into the existing backup and overwrites files with the same name.

Checkpoint: backup/camera_2026-10-24 lists 9 entries (8 files plus raw), and Thumbs.db isn’t one of them.

Test the Python script that copies files

Now we put the pieces together: the glob from Step 2, copy2() from Step 3 and the skip check from Step 4. Save this as deliver_photos.py:

import shutil
from pathlib import Path

SRC = Path("camera")
DST = Path("delivery") / "2026-10-24_Harper"

def deliver(src, dst, pattern="*.jpg"):
    dst.mkdir(parents=True, exist_ok=True)
    copied = skipped = 0
    for photo in sorted(src.glob(pattern)):
        target = dst / photo.name
        if target.exists():          # never overwrite a delivered file
            skipped += 1
            continue
        shutil.copy2(photo, target)  # copy2 keeps the shoot date
        copied += 1
        print(f"copied {photo.name:<13} {photo.stat().st_size / 1024:6.1f} KB")
    print(f"{copied} copied, {skipped} skipped -> {dst}")

deliver(SRC, DST)
print("--- run it again ---")
deliver(SRC, DST)

Output:

copied IMG_2041.jpg    47.1 KB
copied IMG_2042.jpg    50.7 KB
copied IMG_2043.jpg    45.3 KB
copied IMG_2044.jpg    51.9 KB
4 copied, 0 skipped -> delivery\2026-10-24_Harper
--- run it again ---
0 copied, 4 skipped -> delivery\2026-10-24_Harper

That’s the run in the screenshot at the top. The first call creates delivery/2026-10-24_Harper and copies 4 photos, about 45 to 52 KB each. The second call finds all 4 and skips them.

I like printing a count line at the end. When a delivery goes wrong, 0 copied, 4 skipped tells you at a glance that the files were already there.

Pro Tip: the relative paths only work when you run the script from its own folder. If you schedule it, build them from __file__; getting the current directory in Python shows both ways.

Variation: copy files with pathlib Path.copy() in Python 3.14

Python 3.14 added Path.copy() and Path.copy_into(), so you can copy without importing shutil. Same camera folder, run on 3.14.8:

import sys
from pathlib import Path

print("Python", sys.version.split()[0])
dest = Path("delivery")
dest.mkdir(exist_ok=True)
photo = Path("camera/IMG_2043.jpg")

print("copy_into ->", photo.copy_into(dest))
print("copy      ->", photo.copy(dest / "IMG_2043_web.jpg"))

Path("backup").mkdir(exist_ok=True)     # copy() won't create it
tree = Path("camera").copy(Path("backup") / "camera_pathlib")
files = [p for p in tree.rglob("*") if p.is_file()]
print("folder    ->", tree, "with", len(files), "files")

Output:

Python 3.14.8
copy_into -> delivery\IMG_2043.jpg
copy      -> delivery\IMG_2043_web.jpg
folder    -> backup\camera_pathlib with 9 files
VS Code output of Python 3.14 pathlib Path.copy_into and Path.copy copying a photo and the whole camera folder
copy_into() keeps the name, copy() takes the full target path, and copying a folder copies its whole tree.

Both methods return the new path. On a folder, Path.copy() copies the whole tree, so all 9 files land in backup/camera_pathlib, including Thumbs.db, because there’s no ignore option.

One difference from copytree() that tripped my first run: Path.copy() doesn’t create missing parent folders. Without the mkdir() line, it raised FileNotFoundError because backup wasn’t there yet.

They don’t keep the modified date unless you pass preserve_metadata=True. On 3.13 or older, they raise AttributeError, so stick with shutil if your script runs anywhere older.

Can you copy a file with the os module in Python?

Not directly. The os module has no general copy function, which is why shutil exists. You’ll see os.system("copy ...") in old answers, but it’s Windows-only and hides errors.

Use os for the parts around the copy instead: os.makedirs() for the target folder and os.listdir() to find files, as in Step 2. Then let shutil do the copy itself.

Troubleshooting: copy errors we hit

These four mistakes cover most failed copies. Same sample folder, each wrapped in try/except:

import shutil
from pathlib import Path

Path("delivery").mkdir(exist_ok=True)
photo = "camera/IMG_2041.jpg"

tests = {
    "missing source":  lambda: shutil.copy("camera/IMG_9999.jpg", "delivery"),
    "missing folder":  lambda: shutil.copy(photo, "nope/2026/"),
    "copyfile to dir": lambda: shutil.copyfile(photo, "delivery"),
    "same file":       lambda: shutil.copy(photo, "camera"),
}
for label, action in tests.items():
    try:
        action()
        print(f"{label:<16} OK")
    except Exception as err:
        print(f"{label:<16} {type(err).__name__}")

Output:

missing source   FileNotFoundError
missing folder   FileNotFoundError
copyfile to dir  PermissionError
same file        SameFileError
ErrorCauseFix
FileNotFoundError (missing source)Typo in the file name or wrong working directoryBuild paths from __file__; check the name
FileNotFoundError (missing folder)The destination folder doesn’t existCall mkdir(parents=True, exist_ok=True) first
PermissionErrorcopyfile() needs a file path, not a folderUse copy() or add the file name
SameFileErrorSource and destination are the same fileCopy into a different folder

Important: on Windows, copyfile() into a folder raises PermissionError, which looks like a rights problem but isn’t. For the missing-file case, fixing the file does not exist error in Python goes deeper.

Where to Take It Next with your copy script

Once copying works, moving is the natural next step: moving a file in Python with shutil.move() uses the same folder rules but removes the original.

To make the delivery folder name from the shoot date automatically, see creating a directory in Python. For a deeper look at every shutil.copy() argument, read the shutil.copy() method in Python.

If you’re organizing files with Python, these guides pair well with this one:

If the copy is part of a clean-up job, you may want to empty the source folder afterwards. Here is how to delete all files in a directory in Python, including the difference between emptying a folder and removing it with shutil.rmtree().

Frequently asked questions

How do I copy a file to another directory in Python?

Use shutil.copy("photo.jpg", "delivery"). When the second argument is a folder, the file keeps its name.

What’s the difference between shutil.copy() and shutil.copy2()?

Both copy the content and permissions. copy2() also keeps the modified time and other metadata.

How do I copy all files from one folder to another?

Loop over Path("src").glob("*") and call shutil.copy() on each file, or copy the whole tree with shutil.copytree().

Does shutil.copy() overwrite existing files?

Yes, silently. Check target.exists() before copying if you need to keep the existing file.

How do I copy a folder that already exists at the destination?

Pass dirs_exist_ok=True to shutil.copytree() (Python 3.8+). Without it, you get FileExistsError.

Where is the shutil module documented?

See shutil in the Python docs.