[Tutor] just what does read() return?

Alex Hall mehgcap at gmail.com
Fri Oct 1 00:32:40 CEST 2010

Hi all,
I have a parser class which is supposed to take a text file and parse
it. I will then do more with the resulting data. The file is in a
particular format, specified by my professor, though this is not
homework (it will be used to do homework later). The file is in the
vx vy z
vx vy z

where l is either D or U and x, y, and z are numbers. Anyway, I have
the following lines:
  f=open(self.file, "r")
  self.original=f.read() #I thought self.original would now be a
string of all the data in self.file
  txt=str(self.original).split(r"\n+") #create an array where elements
are lines in file
  print txt

I fully expected to see txt be an array of strings since I figured
self.original would have been split on one or more new lines. It turns
out, though, that I get this instead:
['l\nvx vy z\nvx vy z']

How is it that txt is not an array of the lines in the file, but
instead still holds \n characters? I thought the manual said read()
returns a string:

"To read a file's contents, call  f.read(size), which reads some
quantity of data and returns it as a string. size is an optional
numeric argument. When size is omitted or negative, the entire
contents of the file will be read and returned; it's your problem if
the file is twice as large as your machine's memory. Otherwise, at
most size bytes are read and returned. If the end of the file has been
reached, f.read()
 will return an empty string ( ""). "

I know I can use f.readline(), and I was doing that before and it all
worked fine. However, I saw that I was reading the file twice and, in
the interest of good practice if I ever have this sort of project with
a huge file, I thought I would try to be more efficient and read it
once. I will use self.original later again, so I need it either way,
and I figured I could use it since I had already read the file to get
it. TIA.

Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap

More information about the Tutor mailing list