user3527941
user3527941

Reputation: 47

loop over files in python?

I have lots of files that I use glob.glob to sort them to plot. My files are ordered by number: lets say from file.00000 to file.99999.

I use:

filenames = sorted(glob.glob('path/file.*'))
this_next = filenames 

for i, fname1 in enumerate(this_next):
    …

Now I would like to plot every 90 files.

Upvotes: 3

Views: 126

Answers (1)

enrico.bacis
enrico.bacis

Reputation: 31474

Sure, glob and sorted return lists, so in order to get one element every 90 you can use:

from glob import glob

for file in sorted(glob('path/file.*'))[::90]:
    ...

A more memory-efficient solution, if you are sure that glob returns sorted filenames would be to use generators:

from glob import iglob
from itertools import islice

for file in islice(iglob('path/file.*'), 0, None, 90):
    ...

But if you know that the pattern is that statis, for sure the most efficient way would be to generate the file names:

for file in ('file.%5d' % i for i in xrange(0, 99999, 90)):
    ...

Upvotes: 1

Related Questions