from glob import glob l = glob('../???/???.???/*.gz') l.sort() for f in l: ff = f.split('/') print ff[2]