简体   繁体   中英

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. 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) 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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