如何在Python中自动创建数字和连续文件夹?

问题描述 投票:0回答:1

我有一个代码,可以获取单个文件夹的文件,并将它们分批放入新文件夹中,每批 50 个。现在我希望脚本将这些文件夹命名为数字并从 1 开始。 另一种选择是脚本将这些文件放入已经存在的文件夹中,因为我有一个包含空文件夹的模板目录。 需要进行拆分,以便打印软件能够分别输出这些批次。此外,我需要知道哪个文件位于哪个文件夹中。大约有 3800 个 pdf 文件需要排序。 一旦我弄清楚如何获取这些文件夹,我就可以通过在文件夹中创建文件列表来完成项目。

这是目前创建新文件夹并按批处理中的第一个文件命名它们的代码。

import os
import shutil

source_directory = "//All-the-data"
Destination_base_folder = '//All-the-data/sorted'

batch_size = 50

# check if source directory exists
if os.path.exists(source_directory) and os.path.isdir(source_directory):
    # get all the files
    files = os.listdir(source_directory)

 # Sort files by filename 
    
    files.sort()

    counter = 1

    for i in range(0, len(files), batch_size):
        # create a folder for each batch
        batch_directory_name = os.path.splitext(files[i])[0]
        batch_directory_path = os.path.join(Destination_base_folder, batch_directory_name)
        os.makedirs(batch_directory_path, exist_ok=True)

        # copy files into these folders
        for j in range(min(batch_size, len(files) - i)):
            source_file_path = os.path.join(source_directory, files[i + j])
            destination_file_path = os.path.join(batch_directory_path, files[i + j])

            shutil.copy2(source_file_path, destination_file_path)

        print(f"Batch {counter} erfolgreich nach {batch_directory_path} kopiert")
        counter += 1
else:
    print("Quellordner existiert nicht: " + source_directory)

之后我尝试了以下方法来重命名文件夹。哪个文件位于哪个文件夹中并不重要。

def number_folders_chronologically(destination_folder):
    batch_folders = [folder for folder in os.listdir(destination_folder) if os.path.isdir(os.path.join(destination_folder, folder))]
    batch_folders.sort(key=lambda x: os.path.getctime(os.path.join(destination_folder, x)))

    for index, folder in enumerate(batch_folders, start=1):
        old_path = os.path.join(destination_folder, folder)
        new_folder_name = f"{index:03d}_{folder}"
        new_path = os.path.join(destination_folder, new_folder_name)
    os.rename(old_path, new_path)

我对 Python 编程完全陌生,并且已经 15 年没有编程了。即使在那时,我的知识也只是基础。我非常确定解决方案非常简单,但我只是没有看到它。

谢谢大家的帮助。

python file shutil
1个回答
0
投票

脚本现在检查模板目录 (template_directory) 是否存在。如果存在,它将复制第一个文件夹(假设它为空)并使用数字前缀 (batch_X) 重命名它。否则,它会创建具有数字名称(例如batch_001)的新文件夹。字典 (file_to_folder_map) 跟踪哪个文件进入哪个文件夹。处理后,您可以选择打印此映射以供参考。

import os
import shutil

# Define directories
source_directory = '//All-the-data'
destination_base_folder = '//All-the-data/sorted'
template_directory = '//All-the-data/sorted_template'

# Set batch size
batch_size = 50

# Check if source directory exists
if os.path.exists(source_directory) and os.path.isdir(source_directory):

    # Get all files and sort them by filename
    files = os.listdir(source_directory)
    files.sort()

    # Folder numbering and tracking
    folder_number = 1
    file_to_folder_map = {}

    for i in range(0, len(files), batch_size):
        # Use template directory or create a new folder with a number
        if template_directory:
            batch_directory_path = os.path.join(destination_base_folder, os.listdir(template_directory)[0])
            os. shutil.copytree(batch_directory_path, os.path.join(destination_base_folder, f"batch_{folder_number}"))
            batch_directory_path = os.path.join(destination_base_folder, f"batch_{folder_number}")
        else:
            batch_directory_path = os.path.join(destination_base_folder, f"batch_{folder_number:03d}")
            os.makedirs(batch_directory_path, exist_ok=True)

        # Copy files into the folder
        for j in range(min(batch_size, len(files) - i)):
            source_file_path = os.path.join(source_directory, files[i + j])
            destination_file_path = os.path.join(batch_directory_path, files[i + j])
            shutil.copy2(source_file_path, destination_file_path)

            # Track which file goes in which folder
            file_to_folder_map[files[i + j]] = batch_directory_path

        print(f"Batch {folder_number} successfully copied to {batch_directory_path}")
        folder_number += 1

    if file_to_folder_map:
        print("\nFile to Folder Mapping:")
        for filename, folder_path in file_to_folder_map.items():
            print(f"{filename} -> {folder_path}")

else:
    print("Source folder doesn't exist: " + source_directory)
© www.soinside.com 2019 - 2024. All rights reserved.