I'm trying to loop through files I have, and would like to put every two files in a pair, especially that every two files coming after each other are actually related.
I have the files sorted in my directory, and I used the following to loop through the directory and read the pairs of files:
for root, dirs, files in os.walk(TRAIN_DIR):
for file1, file2 in itertools.izip_longest(files[::2], files[1::2]):
However, I receive file1 and file2 in different orders, and not those two files that should come immediately after each other as in the directory. Does os.walk then return unsorted files? What should I do in order to walk through the files in a sorted order?
Thanks.
EDIT 1
This is how I ran files.sort()
:
for root, dirs, files in os.walk(TRAIN_DIR):
files.sort()
for file1, file2 in itertools.izip_longest(files[::2], files[1::2]):