Re: file pointer array
- From: Steven D'Aprano <steve+comp.lang.python@xxxxxxxxxxxxx>
- Date: 05 Jun 2012 04:46:03 GMT
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
.
- References:
- file pointer array
- From: FSH
- file pointer array
- Prev by Date: Re: file pointer array
- Next by Date: How do I get the constructor signature for built-in types?
- Previous by thread: Re: file pointer array
- Next by thread: How do I get the constructor signature for built-in types?
- Index(es):
Relevant Pages
|