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? Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor