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

Recommended Answers

All 7 Replies

a1eio,

What method are you using to read the file? Are you doing something like this?

f = open(filename, "r")
text = f.read()
print text

Is the file very large?

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

>>> f = file("file.txt","r")
>>> f.read()
'\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['
>>> f.read()
'\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'
>>> f.read()
' '
>>>

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

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.

yes on two counts:
1. it looks awfuly messy ;)
2. it was a binary file and it's sorted now :)

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.