readlines() doesn't read entire file

John Machin sjmachin at lexicon.net
Thu Jul 14 20:14:19 EDT 2005


Jeremy wrote:
> I have a most aggravating problem.  I don't understand what is causing 
> readlines() not to read all the lines in the file.

Answer all of Peter Hansen's questions, then read on ...

You are on platform X; did you get the file from platform Y where Y != X?

Where did you get the file from? How was it constructed? What is the 
encoding: ascii/utf-8/iso-something/cp125x/dunno/what's-an-encoding ...

>  I have the following 
> syntax:
> 
> 
> 
> # some initial stuff
> XS = xsdir(Datapath + '/xsdir', options.debug)
> # some more stuff
> 
> class xsdir(object):    #{{{1
>     """This class handles all of the data and methods for reading
>     the xsdir file."""
> 
>     def __init__(self, Datapath, debug=False):
>         self.xsdir = file(Datapath, 'r')        # File object

If you replace 'r' by 'rU', does the behaviour change?

>         self.lines = self.xsdir.readlines()
>         if debug:
>             print self.lines

Add this: print len(self.lines)

>     # and then other stuff as well
> 
> 
> I can see all the lines in the list self.lines,

These are *not* pedantic questions: How many can you "see"? What does 
"see" mean? What is your definition of "line"?

> but they are not all the 
> lines in the file.  When I look at the file in Vim, I can see all the 
> lines,

Repeat above questions.

AND where are the extra lines that you can "see" with vim? In one block 
at the end/beginning/middle? Randomly interspersed? Are you sure you are 
not being confused by a screen-wraparound effect?

  but Python cannot.  Can someone help me with this one?
> Thanks,
> Jeremy
> 

What does "wc -l yourfile" tell you?
[If you are on Windows, get a copy of wc.exe from the gnuwin32 site.]



More information about the Python-list mailing list