I have huge amount of file pairs of the following filename format: <int>-<code>.txt
in a single directory. I need to efficiently loop over the pairs of files with the same <int>
part. I would like to avoid full list evaluation due to the number of files in question. You can assume that every file is guaranteed to be in exactly one pair.
Example
0-A.txt
0-B.txt
1-A.txt
1-B.txt
7-A.txt
7-B.txt
The order is not important, just that matching files are returned. I've tried the:
import glob
A_files = glob.iglob('*-A.txt')
B_files = glob.iglob('*-B.txt')
for A_file, B_file in zip(A_files, B_files):
pass
However, glob has no specified order, so I don't receive matching pairs. Sorting the iterators results in huge lists. Is there an efficient way to loop over matching pairs of files?