By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Reading files with names dynamically changing

P: n/a

Hi all,

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 ?? If so, how should my code look like ?? If it was
not dynamically required, then, I would change the name of the file in my
*open* statement every time and read the corresponding one. But then, I
need everything to be done automatically without my changing the code.

Can someone help me ??
Thanks,

Best Regards,
Satish

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

import time

counter = 1 # First file number
interval = 20 # Time (in seconds) between files

# Run forever

while 1:

# Keep current time

t0 = time.time()

# Open current file and process

print "Reading file number %d..." % counter
f = open( "/path/to/the/files/%07d.dat" % counter, "r" )
process_data_in_file( f )
f.close()
counter = counter + 1

# Sleep until next file

t1 = time.time()
if ( t1 - t0 ) < interval:
time.sleep( interval - ( t1 - t0 ) )
Satish Kumar Chimakurthi wrote:
Hi all,

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 ?? If so, how should my code look like ?? If it was
not dynamically required, then, I would change the name of the file in my
*open* statement every time and read the corresponding one. But then, I
need everything to be done automatically without my changing the code.

Can someone help me ??
Thanks,

Best Regards,
Satish


Jul 18 '05 #2

P: n/a
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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.