User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 402,755 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,623 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 16942 | Replies: 7
Reply
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

reading from a file with python

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Posts: 142
Reputation: G-Do is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 11
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: reading from a file with python

  #2  
Aug 16th, 2005
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?
Vi veri veniversum vivus vici
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: reading from a file with python

  #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
>>> 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\x7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f3\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
Reply With Quote  
Join Date: Jul 2005
Location: France
Posts: 980
Reputation: bumsfeld is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 43
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Posting Shark

Help Re: reading from a file with python

  #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  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: reading from a file with python

  #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  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: reading from a file with python

  #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  
Join Date: Aug 2005
Posts: 2
Reputation: sf2k is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sf2k sf2k is offline Offline
Newbie Poster

Re: reading from a file with python

  #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  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: reading from a file with python

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 8:35 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC