On Mon, 04 Jun 2012 18:21:20 -0700, FSH wrote:

> Hello,
> 
> I have a simple question. I wish to generate an array of file pointers.
> For example, I have files:
> 
> data1.txt
> data2.txt
> data3.txt
> ....
> 
> I wish to generate fine pointer array so that I can read the files at
> the same time.
> 
> for index in range(N):
>      fid[index] = open('data%d.txt' % index,'r')


See the fileinput module:

http://docs.python.org/library/fileinput.html


If you prefer to manage it yourself, you can do something like this:


fid = [open('data%d.txt' % index, 'r') for index in range(1, N+1)]

# Process the files.
for fp in fid:
    print fp.read()

# Don't forget to close them when done.
for fp in fid:
    fp.close()



You can let the files be closed by the garbage collector, but there is no 
guarantee that this will happen in a timely manner. Best practice is to 
close them manually once you are done.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to