reading from a file with python

Thread Solved

Join Date: Aug 2005
Posts: 138
Reputation: a1eio is an unknown quantity at this point 
Solved Threads: 21
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

reading from a file with python

 
0
  #1
Aug 16th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: reading from a file with python

 
0
  #2
Aug 16th, 2005
a1eio,

What method are you using to read the file? Are you doing something like this?
  1. f = open(filename, "r")
  2. text = f.read()
  3. print text
Is the file very large?
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 138
Reputation: a1eio is an unknown quantity at this point 
Solved Threads: 21
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: reading from a file with python

 
0
  #3
Aug 16th, 2005
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
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: reading from a file with python

 
0
  #4
Aug 17th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 138
Reputation: a1eio is an unknown quantity at this point 
Solved Threads: 21
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: reading from a file with python

 
0
  #5
Aug 18th, 2005
yes on two counts:
1. it looks awfuly messy
2. it was a binary file and it's sorted now
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: reading from a file with python

 
0
  #6
Aug 18th, 2005
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 2
Reputation: sf2k is an unknown quantity at this point 
Solved Threads: 1
sf2k sf2k is offline Offline
Newbie Poster

Re: reading from a file with python

 
0
  #7
Aug 23rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: reading from a file with python

 
0
  #8
Aug 23rd, 2005
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC