Reading files with names dynamically changing

Discussion in 'Python' started by Satish Kumar Chimakurthi, Feb 4, 2004.

  1. 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
    Satish Kumar Chimakurthi, Feb 4, 2004
    #1
    1. Advertising

  2. 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
    >
    >
    >
    Aurelio Martin, Feb 4, 2004
    #2
    1. Advertising

  3. Satish Kumar Chimakurthi

    Miki Tebeka Guest

    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
    Miki Tebeka, Feb 4, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paddy McCarthy
    Replies:
    3
    Views:
    708
    Anthony J Bybell
    Sep 24, 2004
  2. Replies:
    2
    Views:
    520
    Chandra Kalle
    Apr 13, 2006
  3. Replies:
    7
    Views:
    658
    Lyell Haynes
    May 2, 2006
  4. Miner Jeff

    file names, reading files

    Miner Jeff, Apr 14, 2008, in forum: C++
    Replies:
    2
    Views:
    324
    Miner Jeff
    Apr 15, 2008
  5. SamF
    Replies:
    4
    Views:
    140
    Stefano Crocco
    Mar 14, 2007
Loading...

Share This Page