Reading files with names dynamically changing

Aurelio Martin amartin at wpsnetwork.com
Wed Feb 4 03:20:43 EST 2004


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
> 
> 
> 




More information about the Python-list mailing list