[Tutor] re-reading file-like objects

Tiago Saboga tiagosaboga at terra.com.br
Mon Oct 9 14:21:42 CEST 2006


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...)?

====================successful==================
import subprocess

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

basefilecontents = 
subprocess.Popen(['dd', 'if=/dev/zero', 'count=1', 'bs=%s' % FILESIZE], 
stdout=subprocess.PIPE, bufsize=FILESIZE).stdout.read()

for i in range(NUMBER):
	print "File number %s" % i
	newfile = open('%s/%s%s' % (DIR, FILENAME, i), 'w')
	newfile.write(basefilecontents)
	newfile.close()

=====================unsuccessful==================

import subprocess, shutil

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

basefile = subprocess.Popen(['dd', 'if=/dev/zero', 'count=1', 'bs=%s' % 
FILESIZE], stdout=subprocess.PIPE, bufsize=FILESIZE).stdout

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

=========================================================
The program runs fine, but only the first file has the appropriate content. 


More information about the Tutor mailing list