简体   繁体   English

如何将多个目录中的文件移动到具有相同子目录的另一个目录

[英]How to move files in multiple directories into another directory with same sub directoies

I have 我有

import shutil

for sub_dir in os.listdir(path + "train"):
    src_files = os.listdir(path+"train/" + sub_dir)
    ...
    for file in src_files[0:split_index]:
        original = path+"train/" + sub_dir+ "/" + file
        distutils.dir_util.mkpath(path + "valid/" + sub_dir)     
        destination = path + "valid/" + sub_dir+"/"
        shutil.move(original, destination)

I have two these directory structures: 我有两个目录结构:

train/abc
train/def
train/ghi

valid/

I need to move some portion of files in train/ to valid/ and retain the original directory structure. 我需要将train /中的文件的某些部分移到valid /并保留原始目录结构。 After the move, valid/ should look the same as train/: 移动后,有效/的外观应与火车/相同:

valid/abc
valid/def
valid/ghi

The above code isn't quite right. 上面的代码不太正确。 How can I modify it to make it right? 如何修改以使其正确?

distutils.dir_util.mkpath(path + "valid/" + sub_dir) 

添加了此部分,现在可以使用。

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

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