[Tutor] re-reading file-like objects (SOLVED?)

Tiago Saboga tiagosaboga at terra.com.br
Mon Oct 9 14:44:54 CEST 2006


I keep the original question:

Em Segunda 09 Outubro 2006 09:21, Tiago Saboga escreveu:
> Hi!
>
> I have a problem with file-like objects for months now, and I hoped I could
> cope with it, but I keep using what seems to me like a newbie workaround...
>
> The question is: how can I read a file (more precisely, a file-like object)
> more than one single time?
>
> In the following example, I want to generate a file in memory and save it
> with ten different names. But I can't do it with shutil.copyfileobj, AFAICS
> because it reads the file with read() method, which can only be used once.
> If it's a real file, on disk, I agree it would not be a clever strategy,
> reading it once for each copy, and I would be happy keeping its content in
> a variable. But if the file is in memory, why can't I simply read it again
> (or better, how can I...)

But I strip the code, and post a new one. The solution I found is in the 
seek() method of the file object. And the problem I had is that not all the 
file-like objects have a functional seek() method. It's not the case for the 
object referenced by the stdout attribute of the subprocess.Popen class. So I 
have to copy it in a (seekable) temporary file. As I have already bothered 
you with my question, let's try to make it still useful: is it the "right"  
(or pythonic) way to do this?

Thanks,

Tiago.

============solution================
import subprocess, shutil, tempfile

FILESIZE = 200000
NUMBER = 10
DIR = '/tmp/pytest'
FILENAME = 'treco.x'

cmd = ['dd', 'if=/dev/zero', 'count=1', 'bs=%s' % FILESIZE]
tempbasefile = tempfile.TemporaryFile()

basefile = subprocess.Popen(
                            cmd, 
                            stdout=subprocess.PIPE, 
                            bufsize=FILESIZE
                           ).stdout

shutil.copyfileobj(basefile, tempbasefile)

for i in range(NUMBER):
	tempbasefile.seek(0)
	print "File number %s" % i
	newfile = open('%s/%s%s' % (DIR, FILENAME, i), 'w')
	shutil.copyfileobj(tempbasefile, newfile)
	newfile.close()


More information about the Tutor mailing list