SoFunction
Updated on 2025-03-04

Implementation example of Python file batch processing operation

In daily development and data processing, we may encounter scenarios where a large number of files need to be operated in batches. For example, batch renaming files, batch moving files, batch modifying file content, etc. Python provides us with a wealth of tools to help us simplify these repetitive operations.

1. Batch rename files

Suppose we have a bunch of files whose names need to be uniform formatted. For example, the file names are in the format of "", "", and we hope to rename them to "photo_1.jpg", "photo_2.jpg".

We can use the os library to implement batch renaming files.

import os

def batch_rename_files(directory):
    # Get all files in the directory    files = (directory)
    
    # traverse the file list and rename it in batches    for index, file in enumerate(files):
        # Generate a new file name        new_name = f"photo_{index + 1}.jpg"
        old_file = (directory, file)
        new_file = (directory, new_name)
        
        # Rename the file        (old_file, new_file)
        print(f"document {file} Renamed to {new_name}")
        
# Call batch rename functionbatch_rename_files("path/to/your/files")

2. Batch moving files

Sometimes, we need to batch move files from one directory to another. We can use the shutil library to implement this functionality easily.

import shutil
import os

def batch_move_files(source_directory, target_directory):
    # Get all files in the source directory    files = (source_directory)
    
    # traverse the file list and move the file to the target directory    for file in files:
        source_path = (source_directory, file)
        target_path = (target_directory, file)
        
        # Move files        (source_path, target_path)
        print(f"document {file} Has been {source_directory} Move to {target_directory}")

# Call batch move functionbatch_move_files("path/to/source_directory", "path/to/target_directory")

3. Bulk modification of file content

If we need to modify the content of a set of files (such as replacing some content in text), we can do it by reading the file content, processing it, and then writing it back to the file.

import os

def batch_modify_files(directory, old_text, new_text):
    # Get all files in the directory    files = (directory)
    
    # traverse the file, read and modify the content    for file in files:
        file_path = (directory, file)
        
        if (file_path):
            with open(file_path, 'r', encoding='utf-8') as f:
                content = ()
            
            # Replace text in file content            content = (old_text, new_text)
            
            with open(file_path, 'w', encoding='utf-8') as f:
                (content)
            print(f"document {file} In-house '{old_text}' has been replaced with '{new_text}'")

# Call batch modification functionbatch_modify_files("path/to/your/files", "oldText", "newText")

4. Error handling and logging

When handling batch file operations, you may encounter various errors, such as the file does not exist, permission problems, etc. Therefore, good error handling and logging are essential.

import os
import logging

# Configure logging(filename='file_operations.log', level=)

def batch_rename_files_with_logging(directory):
    files = (directory)
    
    for index, file in enumerate(files):
        try:
            new_name = f"photo_{index + 1}.jpg"
            old_file = (directory, file)
            new_file = (directory, new_name)
            
            (old_file, new_file)
            (f"document {file} Renamed to {new_name}")
            print(f"document {file} Renamed to {new_name}")
        except Exception as e:
            (f"处理document {file} An error occurred while: {e}")
            print(f"mistake: 处理document {file} An error occurred while: {e}")

# Call a batch rename function with loggingbatch_rename_files_with_logging("path/to/your/files")

Summarize

In this blog, we learned how to use Python to handle batch operations of files, including batch renaming, moving and content modification of files. We also explained how to perform error handling and logging to make the operation more stable and traceable.

Python provides us with simple and powerful file operation tools. Through modules such as os, shutil, and logging, we can easily batch files, greatly improving work efficiency.

This is the end of this article about the implementation example of Python file batch processing operations. For more related contents of Python file batch operation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!