简体   繁体   中英

Parallel Folder Traversal Python

I Have a folder with the following directory structure.

/MainDir/SubDir/20201012/X/1.npy
/MainDir/SubDir/20201012/X/2.npy
/MainDir/SubDir/20201012/Y/1.npy
/MainDir/SubDir/20201012/Y/2.npy
/MainDir/SubDir/20201013/X/1.npy
/MainDir/SubDir/20201013/X/2.npy
/MainDir/SubDir/20201013/Y/1.npy
/MainDir/SubDir/20201013/Y/2.npy

Now if i try to traverse this directory i always exhaust all files within X and then move on to Y. What i want to do is to traverse in this order

/MainDir/SubDir/20201012/X/1.npy
/MainDir/SubDir/20201012/Y/1.npy
/MainDir/SubDir/20201012/X/2.npy
/MainDir/SubDir/20201012/Y/2.npy
/MainDir/SubDir/20201013/X/1.npy
/MainDir/SubDir/20201013/Y/1.npy
/MainDir/SubDir/20201013/X/2.npy
/MainDir/SubDir/20201013/Y/2.npy

Basically i want to traverse a file from X and the corresponding file from Y and after that move to the next file within X and so on. How do i achieve this?

I am currently doing this :

for subdir, dirs, files in os.walk(data_source):
    print('dirs' + repr(dirs))
    for file in files:
        data_path = os.path.join(subdir, file)
        print(data_path)

You can do this:

for dirpath, dirnames, filenames in os.walk(your_dir):

or you can use listdir() function to see the order of directories in which it will traverse.

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