
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy Certain Files from One Folder to Another using Python
Python offers various tools for file manipulation such as shutil module (short for shell utility) which allows us to manipulate files and directories, enabling you to perform various file and directory operations.
Copying Files From one Folder to Another
The goal is to scan a source folder, identify files based on specific criteria, and duplicate them into a target folder. We will utilize Python's standard library modules: 'Os', 'shutil', and 'glob'
- Os: Interacts with the operating system for file system navigation and path management.
- shutil: Provides functions for high-level file operations such as copying, moving, renaming, and deleting files.
- glob: Searches for files in a directory using pattern matching.
Copy All Files
Let's start with a basic example where we copy all files from a source folder to a target folder. We will use the 'os' module to iterate through the files in the source folder and the shuttle module to perform the actual copying.
Example
This example describes a process that takes source and target folder paths. It uses 'os.listdir()' to list files in the source folder, checks if each item is a file with 'os.path.isfile()', and copies it to the target folder using 'shutil.copy()'.
import os import shutil def copy_all_files(source_folder, target_folder): for filename in os.listdir(source_folder): source_file = os.path.join(source_folder, filename) if os.path.isfile(source_file): shutil.copy(source_file, target_folder) copy_all_files('source_folder', 'target_folder')
Output
Following is the output of the above code ?
All files from 'source_folder' will be copied to 'target_folder'.
Copy Files with Specific Extensions
Sometimes, we might only want to copy files with certain extensions for instance, we might want to copy only .txt files or .jpg images. Let's modify our previous example to achieve this.
Example
In this modified version, we added an extension parameter to the function. We then check if each file's name ends with the specified extension before copying it to the target folder.
import os import shutil def copy_files_with_extension(source_folder, target_folder, extension): for filename in os.listdir(source_folder): source_file = os.path.join(source_folder, filename) if os.path.isfile(source_file) and filename.endswith(extension): shutil.copy(source_file, target_folder) copy_files_with_extension("source_folder", "target_folder", ".txt")
Output
Following is the output of the above code ?
Only `.txt` files from `source_folder` will be copied to `target_folder`.
Copy Files Based on Filename Patterns
What if we want to copy files based on specific patterns in their names for instance, we may want to copy all files starting with "report_" or ending with "_final." We can achieve this using the 'glob' module in Python.
Example
In this example, we defined a function that takes the source folder, target folder, and desired pattern as arguments. Used glob.glob() to get a list of file paths that match the specified pattern in the source folder.
import shutil import glob import os def copy_files_with_pattern(source_folder, target_folder, pattern): for file_path in glob.glob(os.path.join(source_folder, pattern)): if os.path.isfile(file_path): shutil.copy(file_path, target_folder) copy_files_with_pattern('source_folder', 'target_folder', 'report_*')
Output
Following is the output of the above code ?
Files that start with `report_` from `source_folder` will be copied to `target_folder`.