| | |
reading from a file with python
Thread Solved |
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
basically is it possible to either skip an eof key and keep reading or specify my own.
thanks
alex
a1eio,
What method are you using to read the file? Are you doing something like this?
Is the file very large?
What method are you using to read the file? Are you doing something like this?
Python Syntax (Toggle Plain Text)
f = open(filename, "r") text = f.read() print text
Vi veri veniversum vivus vici
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
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
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)
>>> 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
•
•
Join Date: Aug 2005
Posts: 2
Reputation:
Solved Threads: 1
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
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
•
•
•
•
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
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.
May 'the Google' be with you!
![]() |
Similar Threads
- RE: reading .wav file and putting ito inot the array (C)
- Reading .dat file (Visual Basic 4 / 5 / 6)
- Reading an input file as a class memeber function (C++)
- reading txt file into array (C++)
- Not reading in entire file. (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- problem reading text file to struct (C++)
- reading and printing a file to screen in C (C)
Other Threads in the Python Forum
- Previous Thread: Testing the Psyco Module to Speed Up Python
- Next Thread: Timing with Module timeit (Python)
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib







