Douglas Alan wrote:
Okay, here's the definitive version (or so say I).  Some good doobie
please make sure it makes its way into the standard library:

def fileLineIter(inputFile, newline='\n', leaveNewline=False, readSize=8192):
   """Like the normal file iter but you can set what string indicates newline.

   You can also set the read size and control whether or not the newline string
   is left on the end of the iterated lines.  Setting newline to '\0' is
   particularly good for use with an input file created with something like
   "os.popen('find -print0')".
   """
   partialLine = []
   while True:
      charsJustRead = inputFile.read(readSize)
      if not charsJustRead: break
      lines = charsJustRead.split(newline)
      if len(lines) > 1:
         partialLine.append(lines[0])
         lines[0] = "".join(partialLine)
         partialLine = [lines.pop()]
      else:
         partialLine.append(lines.pop())
      for line in lines: yield line + ("", newline)[leaveNewline]
   if partialLine and partialLine[-1] != '': yield "".join(partialLine)

|>oug

Hmm, adding optional arguments to file.readlines() would seem to be the place to start, as well as undeprecating file.xreadlines() (with the same optional arguments) for the iterator version.


I've put an RFE (#1152248) on Python's Sourceforge project so the idea doesn't get completely lost. Actually making it happen needs someone to step up and offer a patch to the relevant C code and documentation, though.

Cheers,
Nick.

--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---------------------------------------------------------------
            http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to