[Tutor] differences between mmap and StringIO

Nick Raptis airscorp at otenet.gr
Thu Jul 8 03:45:13 CEST 2010


Actually, for simple file operations I'd neither.
Standard file usage is described here, if you haven't checked it out, 
which I'm sure you have 
http://docs.python.org/library/stdtypes.html#file-objects

StringIO is useful as a buffer. That is, you make a file-like object in 
memory with StringIO, manipulate it as a file, and can then copy it to a 
real file with standard file operations. Really really useful for very 
intensive file operations. Pdf creation comes in mind.

mmap... Well I didn't even know it existed until you mentioned it! Seems 
to be an advanced method of reading a file in memory and manipulating it 
from there with choice of whether it actually affects the physical file 
or not. It's help page says it is also used for communicating with 
subprocesses.. wow! You definately won't need that :)

Anyway, files, StringIOs and mmaps are file-like objects, which means 
they have the same methods and functionality, so you know how to use 
one, you know them all from this aspect.

My recommendation would be, either manipulate a file directly which is 
fine for most cases.

Or if you really want to:
open your file for reading, make a StringIO instance, copy your file to 
it, close the file,
do whatever you want in memory,
open your file again for writing, copy the StringIO to it, close both.
I'd consider that overkill for most projects

Is there something in particular you want to do?

Nick

On 07/08/2010 01:52 AM, Eduardo Vieira wrote:
> Hello, I'm getting confused about the usage of those 2 modules. Which
> should I use one to get/manipulate data from a text file?
>
> Regards,
>
> Eduardo
> www.express-sign-supply.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
>    


More information about the Tutor mailing list