file pointer array

Ben Finney ben+python at benfinney.id.au
Mon Jun 4 21:38:18 EDT 2012


FSH <lee.joosang at gmail.com> writes:

> I have a simple question. I wish to generate an array of file
> pointers.

Welcome to Python.

By your description, I think you want a different type: not an array,
but a list.

I recommend you become familiar with Python's data model
<URL:http://docs.python.org/reference/datamodel.html>.

> I wish to generate fine pointer array so that I can read the files at
> the same time.

I don't know about reading the files *at the same time*.

If you mean you want to have multiple files open simultaneously and read
from any of them arbitrarily, you don't need any particular container
type; you just need to have references to those open file objects.

Any references will do for that; you don't need pointers in Python.

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

Python has iterable types built in, so you rarely need to maintain an
index yourself.

Further, Python has “list comprehension”, allowing you to define a list
in a single statement by describing how each element is created::

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

To learn about this and other useful topics, you should work through
Python's tutorial <URL:http://docs.python.org/tutorial/> from start to
finish, doing each exercise until you understand the concept being
explained.

-- 
 \        “We have to go forth and crush every world view that doesn't |
  `\                believe in tolerance and free speech.” —David Brin |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list