简体   繁体   English

如何将每 500 个文件移动到不同的文件夹中

[英]How to move every 500 files into different folders

As a beginner in Python I would need your help since I do not know enough how to create such script for my need.作为 Python 的初学者,我需要您的帮助,因为我不知道如何根据需要创建这样的脚本。 To give you an overall idea I have a folder Folder_1 that contains 50 000 different frames from a video in .png :为了给你一个整体的想法,我有一个文件夹Folder_1 ,其中包含来自.png视频的 50 000 个不同的帧:

Folder_1 :
    picture_00001
    picture_00002
    picture_00003
    picture_00004
    ...
    picture_50000

Since my Windows explorer is not running quite well with this huge amount of pictures I will need to move all of them in different folders in order to lower my RAM consumption and letting me working on a batch without considering the 50 000 pictures.由于我的 Windows 资源管理器在处理如此大量的图片时运行得不太好,我需要将它们全部移动到不同的文件夹中,以降低 RAM 消耗并让我在不考虑 50 000 张图片的情况下处理批处理。

Therefore my objective is to have a script that will simply move the first 500 files to a folder sub_folder1 and then moving the 500 next to sub_folder2 etc... The folders needs to be created with the script as well :因此,我的目标是拥有一个脚本,该脚本将简单地将前 500 个文件移动到文件夹sub_folder1 ,然后将 500 个文件移动到sub_folder2等旁边......文件夹也需要使用脚本创建:

Folder_1
    sub_folder1
         picture_00001
         picture_00002
         ...
         picture_00500

    sub_folder2
         picture_00501
         picture_00502
         ...
         picture_01000

I started working on with for i in range(500) but I have not clue on what to write then.我开始使用for i in range(500)但我不知道该写什么。

Hopping this is clear enough otherwise let me know and I will do my best to be even more precised.跳得够清楚了,否则让我知道,我会尽力做到更精确。

Thank you in advance for your help.预先感谢您的帮助。

One possible solution is:一种可能的解决方案是:

First you find out which are the .png file names in the directory.首先找出目录中的.png文件名。 You can achieve this by using os.listdir(<dir>) to return a list of filenames, then iterate over it and select just the correct files with fnmatch .您可以通过使用os.listdir(<dir>)返回文件名列表来实现此目的,然后对其进行迭代并使用fnmatch仅选择正确的文件。

Then you set the increment (in this example 10 , in yours 500 ), iterate over a generator range(0, len(files), increment) , create a folder just if it doesn't exist and then move the files in chunks.然后设置增量(在本例中为10 ,在您的500 ),迭代生成器range(0, len(files), increment) ,创建一个文件夹,如果它不存在,然后将文件分块移动。

Solution:解决方案:

from fnmatch import fnmatch
import os
import shutil

def get_filenames(root_dir):
    pattern = '*.png'
    files = []
    for file in os.listdir(root_dir):
        if fnmatch(file, pattern):
            files.append(file)
    return files

def distribute_files():
    root_dir = r"C:\frames"
    files = get_filenames(root_dir)
    increment = 10

    for i in range(0, len(files), increment):
        subfolder = "files_{}_{}".format(i + 1, i + increment)
        new_dir = os.path.join(root_dir, subfolder)
        if not os.path.exists(new_dir):
            os.makedirs(new_dir)
        for file in files[i:i + increment]:
            file_path = os.path.join(root_dir, file)
            shutil.move(file_path, new_dir)


if __name__ == "__main__":
    distribute_files()

Hope it helps.希望能帮助到你。

Regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM