File organization is a crucial task in various domains like digital asset management, content development, and data analysis. Often, there is a need to pair related files, such as text documents with their corresponding images. This article explores a Python script designed to automatically organize paired .txt
and .png
files into respective folders, and move unpaired files to a designated “Not found” folder. Such automation enhances efficiency, especially when dealing with large datasets.
Python Script for Organizing Paired Files
Purpose of the Script
The script’s primary function is to sift through a directory containing both text and image files, pair them based on their filenames, and organize them into individual folders. This helps maintain a clean directory structure and ensures related files are grouped together.
Script Breakdown
Defining the Function
def organize_pairs(folder_path):
# Function code will be detailed below
The organize_pairs
function is crafted to manage the file organization task. It accepts one argument, folder_path
, which specifies the directory path containing the files to be organized.
Creating Directories for Unmatched Files
not_found_path = os.path.join(folder_path, "Not found")
os.makedirs(not_found_path, exist_ok=True)
Here, the script creates a directory for storing unmatched files. The os.makedirs
function facilitates the creation of this directory and exist_ok=True
prevents any error if the directory already exists.
Listing and Sorting Files
files = os.listdir(folder_path)
txt_files = [f for f in files if f.endswith('.txt')]
img_files = [f for f in files if f.endswith('.png')]
The script lists all files in the provided folder_path
and categorizes them into text files and image files using list comprehensions. This separation is essential for the following pairing logic.
Pairing and Organizing Files
for txt_file in txt_files:
base_name = os.path.splitext(txt_file)[0]
img_file = f"{base_name}.png"
if img_file in img_files:
pair_folder = os.path.join(folder_path, base_name)
os.makedirs(pair_folder, exist_ok=True)
shutil.move(os.path.join(folder_path, txt_file), os.path.join(pair_folder, txt_file))
shutil.move(os.path.join(folder_path, img_file), os.path.join(pair_folder, img_file))
Each text file is processed to find its corresponding image file. If found, a new folder is created for the pair, and both files are moved to this new location.
Handling Unpaired Files
if img_file not in img_files:
shutil.move(os.path.join(folder_path, txt_file), os.path.join(not_found_path, txt_file))
Any text file without a matching image file is moved to the “Not found” directory. The script also checks for any unpaired image files and moves them accordingly.
Example Usage
organize_pairs('E:/Python/pair-added')
To utilize the script, simply call the organize_pairs
function with the path to the directory containing your files.
Conclusion
This Python script provides a robust solution for organizing paired files efficiently. It automates the tedious process of manually searching and pairing related files, significantly enhancing productivity and ensuring a well-organized file system. Python’s powerful file handling capabilities make it an excellent choice for implementing such file management tasks.
import os
import shutil
def organize_pairs(folder_path):
# Create a directory for unmatched files
not_found_path = os.path.join(folder_path, "E:/Python/Not found")
os.makedirs(not_found_path, exist_ok=True)
# Gather all files and sort them for pairing
files = os.listdir(folder_path)
txt_files = [f for f in files if f.endswith('.txt')]
img_files = [f for f in files if f.endswith('.png')]
# Create a set for paired files to skip already paired files in the main loop
paired_files = set()
# Loop through txt files to find their png pair
for txt_file in txt_files:
base_name = os.path.splitext(txt_file)[0]
img_file = f"{base_name}.png"
if img_file in img_files:
# Create a pair folder
pair_folder = os.path.join(folder_path, base_name)
os.makedirs(pair_folder, exist_ok=True)
# Move both files to the new folder
shutil.move(os.path.join(folder_path, txt_file), os.path.join(pair_folder, txt_file))
shutil.move(os.path.join(folder_path, img_file), os.path.join(pair_folder, img_file))
paired_files.update([txt_file, img_file])
print(f"Moved {txt_file} and {img_file} to {pair_folder}")
else:
# Move the unpaired txt file to the 'Not found' folder
shutil.move(os.path.join(folder_path, txt_file), os.path.join(not_found_path, txt_file))
print(f"Moved {txt_file} to {not_found_path}")
# Check for unpaired png files
for img_file in img_files:
if img_file not in paired_files:
# Move the unpaired image file to the 'Not found' folder
shutil.move(os.path.join(folder_path, img_file), os.path.join(not_found_path, img_file))
print(f"Moved {img_file} to {not_found_path}")
# Example usage
organize_pairs('E:/Python/pair-added')