Reading files with names dynamically changing

  • Thread starter Satish Kumar Chimakurthi
  • Start date
S

Satish Kumar Chimakurthi

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
 
A

Aurelio Martin

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

Miki Tebeka

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top