[ALUG] Python question about reading files

Jan T. Kim jtk at cmp.uea.ac.uk
Tue Nov 6 19:44:53 GMT 2007


On Tue, Nov 06, 2007 at 07:03:25PM +0000, Chris G wrote:
> On Tue, Nov 06, 2007 at 04:21:19PM +0000, Jon Dye wrote:
> > On 06/11/2007, Chris G <cl at isbd.net> wrote:
> > > Are there any Python people here?
> > >
> > > How do I do the equivalent of the C below in Python :-
> > >
> > >     while ( (str = read(file)) != NULL)
> > >     {
> > >         do something;
> > >     }
> > 
> > I'm not sure I understand your c example.  The c read function returns
> > the number of bytes read or -1 on an error.  It's declaration is:
> > 
> > ssize_t read(int fd, void *buf, size_t count);
> > 
> Yes, as I said before, it wasn't right, what I meant was to have the
> parameters as you say with a fairly small 'count'.  The the loop
> iterates until EOF.
> 
> 
> > > I can read the data OK, i.e. I have opened the file and I can
> > > successfully read from the file with the line:-
> > >
> > >     tagNum = tlvFile.read(3)
> > 
> > That will read at most 3 bytes from the file.  If you've reached EOF
> > then it will return the empty string.  The empty string returns false
> > if evaluated in a boolean context so:
> > 
> > tagNum = tlvFile.read(3)
> > while tagNum:
> >     # do stuff with tagNum
> >     tagNum = tlvFile.read(3)
> > 
> > would probably work.  Not sure how you are intending to handle your
> > variable record length stuff in a while loop though.
> > 
> Yes, I just don't feel those two tlvFile.read(3) calls are very
> elegant somehow.

If elegance is the issue, objects are perhaps a solution; you can write
yourself a little class that allows you to iterate over the file using a
for loop:

class FileSequence(object) :

  def __init__(self, f) :
    self.f = f


  def __iter__(self) :
    return self


  def next(self) :
    s = self.f.read(3)
    if s == '' :
      raise StopIteration
    return s


fs = FileSequence(file(sys.argv[1], 'r'))
for w in fs :
  print w

Obviously, the next method can read and return chunks of variable length
and instance variables can be used for any state required to work out
chunk lengths or perhaps to buffer stuff... so this might even be useful
in addition to being elegant... in fact, pretty neat lexical scanners
can be written this way.

Best regards, Jan
-- 
 +- Jan T. Kim -------------------------------------------------------+
 |             email: jtk at cmp.uea.ac.uk                               |
 |             WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*



More information about the main mailing list