我有一个代码,采取一个文件夹的文件,并把他们在批50到新的文件夹.现在我想脚本命名这些文件夹数字和开始与1. 另一种 Select 是脚本将这些文件放在已经存在的文件夹中,因为我有一个模板目录,文件夹为空. 拆分是必要的,这样打印软件将能够吐出这些批次单独.此外,我需要知道哪个文件在哪个文件夹.大约有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)

之后,我try 了以下重命名文件夹的方法.哪个文件在哪个文件夹中并不重要.

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年没有编程了.即使是在那个时候,我的知识也是基本的.我非常确定解决方案是完全容易的,我只是看不出来.

谢谢大家的帮助.

推荐答案

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

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)

Python相关问答推荐

将数据框架与导入的Excel文件一起使用

log 1 p numpy的意外行为

如何将Docker内部运行的mariadb与主机上Docker外部运行的Python脚本连接起来

avxspan与pandas period_range

基于索引值的Pandas DataFrame条件填充

如何让这个星型模式在Python中只使用一个for循环?

python panda ExcelWriter切换动态公式到数组公式

在Python中使用yaml渲染(多行字符串)

在Python中从嵌套的for循环中获取插值

并行编程:同步进程

Python日志(log)模块如何在将消息发送到父日志(log)记录器之前向消息添加类实例变量

如何删除重复的文字翻拍?

判断Python操作:如何从字面上得到所有decorator ?

jsonschema日期格式

有没有办法在不先将文件写入内存的情况下做到这一点?

分解polars DataFrame列而不重复其他列值

以极轴表示的行数表达式?

启动线程时,Python键盘模块冻结/不工作

时长超过24小时如何从Excel导入时长数据

如何将参数名作为参数传入到函数中?