How to copy certain files from one folder to another using Python?



When we are working with files in Python we may need to copy only a certain type of file. Python makes this task simple and flexible by using built-in modules such as os and shutil and pathlib.

Copying specific files using shutil and os modules

When we want to copy only certain files such as all .txt files from one folder to another in Python then we can use the shutil and os modules. The shutil module is used to perform high-level operations on files such as file copying, moving, archiving and removing directories whereas os module is used to interact with the operating system to perform tasks such as file/directory manipulation, environment management and process handling.

Example

Here's an example, which copies all files ending with .txt from a source folder to a destination folder with the help of os.makedirs() and shutil.copy() methods to locate and copy the files in python -

import os
import shutil

source_folder = r'D:\Tutorialspoint\Articles\source_dir'
destination_folder = r'D:\Tutorialspoint\Articles\dest_dir'

# Make sure the destination directory exists
os.makedirs(destination_folder, exist_ok=True)

# Loop through the files in the source directory
for file in os.listdir(source_folder):
    if file.endswith('.txt'):
        source_path = os.path.join(source_folder, file)
        destination_path = os.path.join(destination_folder, file)
        shutil.copy(source_path, destination_path)
print("All text files are copied")

This program checks each file in the source directory and copies only those files that end with .txt -

Filtering Files by Name or Keyword

When we want to copy files that contain a specific word in their name, for example, all files that contains the word "report" then we need to loop all the files to check for the defined condition. Following is the example which filters the files by their name or defined keyword -

keyword = 'report'

for file in os.listdir(source_folder):
    if keyword in file:
        shutil.copy(os.path.join(source_folder, file),
                    os.path.join(destination_folder, file))
print("All text files are copied")

Note: This approach is useful when filenames follow naming patterns or contain identifiers.

Copying specific files using pathlib and shutil modules

When we want to copy only certain files such as all .txt files from one folder to another in Python, we can use the pathlib module along with the shutil module. The pathlib module provides an object-oriented interface for filesystem paths whereas shutil module is used to perform file operations such as copying, moving and removing files or directories.

Example

Following is an example that uses the Path object from the pathlib module to find and copy all .txt files from a source folder to a destination folder with the help of mkdir() and shutil.copy() methods in Python -

from pathlib import Path
import shutil

source_folder = Path(r'D:\Tutorialspoint\Articles\source_dir')
destination_folder = Path(r'D:\Tutorialspoint\Articles\dest_dir')

# Create the destination folder if it doesn't exist
destination_folder.mkdir(parents=True, exist_ok=True)

# Loop through all .txt files in the source directory
for file_path in source_folder.glob('*.txt'):
    shutil.copy(file_path, destination_folder / file_path.name)
print("All text files are copied.")
Updated on: 2025-09-01T14:53:40+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements