Reputation: 10485
I am iterating a bunch of files like so:
for file in glob('./*.dat'):
print file
And the output is always the following:
./SAN0.dat
./SAN4.dat
./SAN1.dat
./SAN2.dat
./SAN3.dat
./SAN5.dat
./SAN6.dat
./SAN7.dat
How can I iterate them in order of their name, (meaning SAN1.dat
would be second, for example)?
Thanks!
Upvotes: 1
Views: 102
Reputation: 141
Following is the easiest way to iterate the files in order of filenames in python-
import os
for file in sorted(os.listdir(path))
Upvotes: 0