[Tutor] Questions on file.read

Max Noel maxnoel_fr at yahoo.fr
Thu Jul 14 15:17:47 CEST 2005


On Jul 14, 2005, at 12:26, Negroup - wrote:

> "To read a file's contents, call f.read(size), which reads some
> quantity of data and returns it as a string. size is an optional
> numeric argument. When size is omitted or negative, the entire
> contents of the file will be read and returned; it's your problem if
> the file is twice as large as your machine's memory."
>
> What does exactly mean that it's my problem (crash? an exception will
> be raised? fire and flames? xyz?). How can my recognize a "too big
> file" before read it?

     Python will try to read the entire file in memory. Now if the  
file is twice as large as your computer's memory, it will run out of  
it, and then switch to swap space (virtual memory) to fit the rest.
     You know how when you're running a resource-intensive game,  
sometimes the action freezes and you hear your hard drive thrashing  
like crazy? Same thing here. Things will slow down to a crawl. If the  
machine is a server, it's obviously a Bad Thing -- however, the box  
shouldn't crash.
     Things really get ugly when you run out of swap space. Most OS's  
set the swap file to somewhere between 1 and 2 times your actual RAM  
-- that is, if you have 256 megs of RAM, you can assume that anything  
that requires up to 640 megs will work (albeit slowly).
     Now if you manage to fill up both the physical RAM and the swap  
space, there won't be any more space to finish reading the file. If  
that happens, chances are your machine will crash. If it doesn't,  
Python will raise a MemoryError.

     In any case, this is something you *don't* want to happen. In  
the big hierarchy of Bad Things, there is only one thing worse than a  
MemoryError:

 >>> import sys
 >>> sys.is_computer_on_fire()
True


     In any cas, you should try to avoid using file.read without a  
size parameter. If you're processing text files, reading them one  
line at a time would be a good start (for line in open 
('filename.txt'): is an instance of Best Thing Ever).


-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting  
and sweating as you run through my corridors... How can you challenge  
a perfect, immortal machine?"


	

	
		
___________________________________________________________________________ 
Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger 
Téléchargez cette version sur http://fr.messenger.yahoo.com


More information about the Tutor mailing list