mimicking a file in memory

Tim Chase python.list at tim.thechases.com
Tue Nov 20 16:49:33 EST 2007


> I thought about this approach originally, but here's the catch
> there: the read method isn't the only method i need. mutagen
> calls the seek method on the file object. urllib2 returns a
> "file-like object" that does not have a seek method associated
> with it, which means i'd have to extend urllib2 to add that
> method. Problem is, i don't know how you could implement a
> seek method with urllib2.

It sounds like you're almost reaching for the StringIO library 
(or the similar cStringIO library).

 >>> URL = 'http://python.org/styles/print.css'
 >>> from StringIO import StringIO
 >>> from urllib2 import urlopen
 >>> u = urlopen(URL)
 >>> s = StringIO(u.read())
 >>> u.close()
 >>> s.seek(24)
 >>> s.read(6)
'google'

Hope this helps,

-tkc










More information about the Python-list mailing list