Reading files with names dynamically changing

Miki Tebeka miki.tebeka at zoran.com
Wed Feb 4 06:37:44 EST 2004


Hello Satish,
> An external solver program is dynamically producing files with different
> names 0000001.dat, 0000002.dat, 0000003.dat etc.....at regular intervals.
> These files contain all numeric data. Is it possible to read each of these
> dynamically in python ??
What do you mean?
If you want to find all *.dat file use the `glob' module.

> If so, how should my code look like ?? 
#!/usr/bin/env python

from glob import glob
from os.path import getmtime

files = glob("./*.dat") # Find all files ending with .dat in current directory
# Sort by modification time, newest first
files.sort(lambda f1, f2: cmp(getmtime(f2), getmtime(f1)))
# Do something with the files
for file in files:
    print file

HTH.
Miki



More information about the Python-list mailing list