473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reading files with names dynamically changing


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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matthew Cascio | last post by:
My understanding is that using reserved words as column names is allowable as long as they are quoted. I am trying to create a table dynamically with columns defined by the first row of a text...
1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
3
by: | last post by:
I am a novice in C# and need help. I want to write a simple program to read a bunch of files from a specified directory and rename those files in a sequential fashion., changing a bunch of image...
4
by: Daniel Nogradi | last post by:
Is it possible to have method names of a class generated somehow dynamically? More precisely, at the time of writing a program that contains a class definition I don't know what the names of its...
2
by: dreamer29 | last post by:
I want to find out the unused function names from the object files that are dynamically linked later.the object files are generated from C code. Please tell me some tool to generate the call...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
4
by: Miner Jeff | last post by:
Hello, I have a basic question about reading files. I have several data files where the filenames are identical except for a short (3 character) prefix. I inherited this code and the person who...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.