[Tutor] Load Entire File into memory

Amal Thomas amalthomas111 at gmail.com
Mon Nov 4 14:06:05 CET 2013


Hi,

Thanks Alan.

Now I have made changes in code :

Present code:





*f = open("output.txt")content=f.read().split('\n') f.close()for lines in
content:*
*  <processing>*
*content.clear()*

Previous code:






*f = open("output.txt") content=io.StringIO(f.read()) f.close()for lines in
content:  <processing>*
*content.close()*


   Now I have found that memory use is roughly 1.5 times the size of text
file. Previously it was around 4-5 times. Its remarkable change. Waiting
for more suggestions.

Thanks,



On Mon, Nov 4, 2013 at 5:05 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> On 04/11/13 11:07, Amal Thomas wrote:
>
>     I am currently using this method to load my text file:
>> *f = open("output.txt")
>> content=io.StringIO(f.read())
>> f.close()*
>>
>>   But I have found that this method uses 4 times the size of text file.
>>
>
> So why not use
>
>
> f = open("output.txt")
> content=f.read()
> f.close()
>
> And process the file as a raw string?
>
> Is there a reason for using the StringIO?
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 



*AMAL THOMASFourth Year Undergraduate Student Department of Biotechnology
IIT KHARAGPUR-721302*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131104/2d5a8086/attachment.html>


More information about the Tutor mailing list