如何在Python中自动创建数字和持续文件夹?
我有一段代码,它可以把一个文件夹里的文件分成每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年没编程了。即使在那时,我的知识也很基础。我很确定解决方案应该很简单,只是我没看出来。
谢谢大家的帮助。
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)