简体   繁体   English

如何将某个目录中所有文件的一定百分比移动到另一个目录中?

[英]How to move a a certain percentage of all the files in a directory into another directory?

I have like the following datastructure: 我喜欢以下数据结构:

-folder1
--subfolder1
--subfolder2
--ytz
--subfolder128
-folder2
--subfolder1
--subfolder2
--ytz
--subfolder128

in each of these subfolders of folder1 there are different number of files. 在folder1的每个子文件夹中,文件数量不同。 I want 10% of these files moved to corresponding subfolder of folder2 and have them delete in the corresponding folder of folder1. 我想将这些文件中的10%移到folder2的相应子文件夹中,并删除它们在folder1的相应文件夹中。

All of the subfolders (from folder1/folder2) are named the same. 所有子文件夹(来自folder1 / folder2)的名称均相同。

I know how to move files from one directory to another 我知道如何将文件从一个目录移动到另一个目录

for i in range(len(synonym_list)):
    dest1 = '../data/mushrooms_with_seperates_synonms/'+synonym_list[i][0]+'/'
    for j in range(len(synonym_list[i])):
        if (j != 0):
            source = '../data/mushrooms_with_seperates_synonms/'+synonym_list[i][j]+'/'
            files = os.listdir(source)
            for f in files:
                    shutil.move(source+f, dest1)
            shutil.rmtree(source)
for i in range(len(synonym_list)):
    dest1 = '../data/mushrooms_with_seperates_synonms/'+synonym_list[i][0]+'/'
    for j in range(len(synonym_list[i])):
        if (j != 0):
            source = '../data/mushrooms_with_seperates_synonms/'+synonym_list[i][j]+'/'
            files = os.listdir(source)
            for k, f in enumerate(files):
                if not k % 10:
                    shutil.move(source+f, dest1)

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

相关问题 如何将文件从一个目录移动到另一个目录? - How to move files from a directory to another? 将某些文件从一个目录移动到另一个目录-Python - Move Certain Files from One Directory to Another - Python Python 将目录中的所有文件移动到另一个目录中带有时间戳的子目录 - Python move all files in directory to sub-directory with timestamp in another directory 将所有文件从一个目录移动到另一个磁盘空间有限的目录 - Move all files from one directory to another with limited disk space Python递归移动子目录下的所有文件到另一个目录 - Recursively move all files on subdirectories to another directory in Python 如何使用 Python 脚本将某些类型的文件移动到其他目录 - How to move certain types of files to other directory with a Python script 将所有文件从目录树移动到另一个目录。 重命名,如果重复名称 - Move all files from a directory tree to another directory. Rename if repeated names 如何将多个目录中的文件移动到具有相同子目录的另一个目录 - How to move files in multiple directories into another directory with same sub directoies 如何有条件地将文件从一个目录移动到另一个目录? - How to conditionally move files from one directory to another? 使用 Paramiko 将文件从一个目录移动到另一个目录 - Move files from one directory to another with Paramiko
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM