> 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...)?
Not all file like objects support the seek() call, often for good reasons - they are streams for example. However it sounds like you could use a StringIO buffer object for what you want and you can reuse that as often as you like. Look here for more info: http://docs.python.org/lib/module-StringIO.html HTH -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld > > ====================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. > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor