[Tutor] re-reading file-like objects

Tiago Saboga tiagosaboga at terra.com.br
Mon Oct 9 19:54:42 CEST 2006


Em Segunda 09 Outubro 2006 10:41, Kent Johnson escreveu:
> Tiago Saboga wrote:
> > 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()
>
> Your input file isn't 'in memory', it is the output of a subprocess. To
> read the input again, the subprocess would have to create the output again.
>
> This solution seems fine to me - you read the input file *contents* into
> memory, then write it to multiple files. Why don't you like this approach?

I'm afraid I don't fully understand file objects. I thought I could use it 
just as a file: if I have one, and I want several copies, I just save it with 
several names. So, if Popen.stdout already is a file object, I thought the 
easier way would be to save it with another name. I see that it is more 
complicated.
For this simple example, this solution is fine, and I think the solution 
proposed by Alan (StringIO) is not going to add anything. Would it be faster?

Thanks,

Tiago.
> Kent
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list