[Tutor] files

Ricardo Aráoz ricaraoz at gmail.com
Sun Sep 2 02:53:16 CEST 2007


Alan Gauld wrote:
> "Ricardo Aráoz" <ricaraoz at gmail.com> wrote
> 
>>>>> In = open(r'E:\MyDir\MyDoc.txt', 'rb')
>>>>> Out = open(r'E:\MyDir\MyUpperDoc.txt', 'wb')
>>>>> Out.write(In.read().upper())
>>>>> In.close()
>>>>> Out.close()
>> Pretty simple program. The question is : If 'In' is a HUGE file, how
>> does Python process it?
> 
> Exactly as it does for a small file... :-)
> 
>> Does it treat it as a stream and passes bytes to
>> 'Out' as soon as they are coming in, or does it read the whole file 
>> into
>> memory and then passes the whole file to 'Out'?
> 
> You have told it to do the latter.
> read() reads the whole file into a string so
> 
> Out.write(In.read().upper())
> 
> Is exactly the same as
> 
> temp = In.read()
> temp = temp.upper()
> Out.write(temp)
> 
> Just because you put it in one line doesn't chanhge how
> Python interprets it.
> 
>> If the answer is the first choice I would like to know how to 
>> instruct
>> Python to do the second choice.
> 
> I'm guessing you mean this the other way around?
> 
> You can read the file line by line
> 
> for line in In:
>     Out.write(line.upper())
> 
> HTH,


Thanks a lot.





More information about the Tutor mailing list