944,191 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 95952
  • Python RSS
Aug 16th, 2005
0

reading from a file with python

Expand Post »
I want to read everything from a file using python. the one problem i am having (note: EVERYTHING from a file) is that python is returning an EOFError when i know that it's not the end of the file, is there any way to just read the damn thing till the computer crashes or at least untill the file i want to read from has been literaly sucked dry.

basically is it possible to either skip an eof key and keep reading or specify my own.

thanks
alex
Similar Threads
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Aug 16th, 2005
0

Re: reading from a file with python

a1eio,

What method are you using to read the file? Are you doing something like this?
Python Syntax (Toggle Plain Text)
  1. f = open(filename, "r")
  2. text = f.read()
  3. print text
Is the file very large?
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005
Aug 16th, 2005
0

Re: reading from a file with python

yes
when i do text = f.read()
then when i print text i get a little bit then if i do it again i get a load more then if i try again i don't get anything and i am positive that there is still much much more
Python Syntax (Toggle Plain Text)
  1. >>> f = file("file.txt","r")
  2. >>> f.read()
  3. '\x17\xa9\xaa\t\xb5\x11%$\xe9e\xcf\xaf\x1e\xee\xcf\xcb\xab\xef\xd7_r\x95\x94^ \xa8\x9e\x9b\xe4+\xcen\x9fY^\xeaI\x13\xbc\x08\xb4G\x7fiHP['
  4. >>> f.read()
  5. '\xa8\xca\xc9)\xde\xfa7|9l\xdc%\xe6\x17\xc5\x96F6\xc8Jx&\xf4\x0b\xb2&\x87\xbe\xe5\xeb>\x0f\xf6\xa7t\xa5"m\xfeM\xbd\x9f\x18\xbd\xf5MVT\xb2 \x13\x94S\xd0\x1c\x9b\\\xb9.g\xcb\x80\x05.-l\x87\x92&e\xd9\xf3\x9c4\xdb\xb0\x13\xee\xd8\x98vA\xf6U\xfdp/\x199\xc3A^\nC\x97\xe7\x9d`i\xf3\x1f\x89\x19\xb8:<2\x1f$\x8b\xe1\xe5K\xb4c\x03S\xbcB\x81\x92\xa9\xd0\xb4\xaaW\xcd\xf3ds\x8c\xb5\x87\xa5\x97L\xc3r0\rX\xd9\x8d(\xac=L\xfdp\x95\xf38yw\xf3e\xff\x07\x08pc=j\xac%\xad\xb7\xc9-6\xefi%\xb7\x7f\xb0\xe3H\xaf\x8f\xbf\x04k\x0b\xc5\xe4\xc9R\xa2\xc4.\xe3F\xfd\x84\x94\x01\xbeb\xaf5\x93\x95\tG+\xe5;\xfe\xe7\x92\xe6\xee8W0\x19NyEu*\xb1>\x7f\x9bc\x11q\x1d\x0c\xe9^:K\xab\xc5dt\xeb\xd4\xd7\x02\x1c\xf6\x8c\xecB\xdb\xb83\xef\xa2\xfb\x89\xd6J\xa6\x98\xaf\x126\xad\xc6\xf6\x84F\xf5\xed\xb8fL\x04|\x130\x18\x7f\xeaH?\xd2-\xed\x9b#6l\xa7W\xbd\xe1\xc4\x9d\xc8\x01\x13s\xbfwU\xa6\x80P\xf3\xdf\xe4\t\xd7\xb9\x96\xae<\xf1\xa6\xbf\x19\xb2\xc0&d\x8bt\x06\xb6\x10i\xb8\x90\xb1\xfb\xa4\xd9\xd5\x0fR\xd2\xb7\x9ax\t\r\xc0K\x92\x13\xe7\xcf\x1e\xe4\xb9\x91\x12\xcbe_\\r"w\x13\x8fF\xdc\xa1\x14\xe6\x16\x96]b7\xc1q\xbdY\xcb\xdb@q\xf3\x1d8\xa8\rHB'
  6. >>> f.read()
  7. ' '
  8. >>>

as you can see it does it once then it does it again
so i need some kind of method to keep doing it over and over again untill the whole file has been sucked dry
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Aug 17th, 2005
0

Re: reading from a file with python

Can you display that file on an editor? Are you sure you don't have a binary file? In that case use 'rb'

Using the python shell makes things look messy.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Aug 18th, 2005
0

Re: reading from a file with python

yes on two counts:
1. it looks awfuly messy
2. it was a binary file and it's sorted now
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Aug 18th, 2005
0

Re: reading from a file with python

Actually using the Python Shell in this case gave the clue that this was not a text file, since most of the ASCII text characters are less than hex A0.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 23rd, 2005
0

Re: reading from a file with python

try f.readlines() instead of f.read()

f.read() is basically doing it's job, reading one line at a time. With readlines() though, the whole thing becomes an object that you can then iterate through or search etc.

i think the online python manual also has some example differences.

hope that helps
sf2k
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sf2k is offline Offline
2 posts
since Aug 2005
Aug 23rd, 2005
0

Re: reading from a file with python

Quote originally posted by sf2k ...
try f.readlines() instead of f.read()

f.read() is basically doing it's job, reading one line at a time. With readlines() though, the whole thing becomes an object that you can then iterate through or search etc.

i think the online python manual also has some example differences.

hope that helps
sf2k
Some basics:
read() reads to EOF, or if opened as a binary, to the last byte
read(n) reads n bytes
readline() reads a line at a time, the line ends with '\n' or EOF.
readlines() uses readline() to read all the lines in a file and returns a list of lines.

The first three functions return a string object, the last function returns a list object. EOF is a special character used as End Of File marker.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Testing the Psyco Module to Speed Up Python
Next Thread in Python Forum Timeline: Timing with Module timeit (Python)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC