Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reading files with names dynamically changing

Reply
Thread Tools

Reading files with names dynamically changing

 
 
Satish Kumar Chimakurthi
Guest
Posts: n/a
 
      02-04-2004

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



 
Reply With Quote
 
 
 
 
Aurelio Martin
Guest
Posts: n/a
 
      02-04-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
>
>
>


 
Reply With Quote
 
 
 
 
Miki Tebeka
Guest
Posts: n/a
 
      02-04-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
file names, reading files Miner Jeff C++ 2 04-15-2008 07:32 PM
Copying files and changing their names SamF Ruby 4 03-14-2007 05:42 PM
reading files names anthonyJ Computer Support 5 11-28-2006 08:21 PM
reading from "unknown" number and names of files using fstream jccorreu@gmail.com C++ 7 05-02-2006 03:50 PM
How to find out the names of uncalled functions from object files that are dynamically linked? dreamer29@gmail.com C Programming 2 04-13-2006 08:56 AM



Advertisments