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

Kent Johnson kent37 at tds.net
Mon Oct 9 15:43:39 CEST 2006

Tiago Saboga wrote:
> 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()

This is similar to your first successful solution in a way - you are 
reading the contents of the input file and storing them in a temporary 
location. In the first version you stored the contents in memory; in 
this one you store the contents in a temp file. Unless the data is too 
large to fit in memory I think the first program is preferable, it is 
simpler and will likely be faster.


More information about the Tutor mailing list