Message50852
Hi,
tempfile.NamedTemporaryFile provides a good interface
to creating temporary files, but its insistence on
deleting the file upon close is limiting. The attached
patch adds an optional parameter to NamedTemporaryFile
that allows persistence of the temp file after it has
been closed.
One use-case that demonstrates where keeping the
temporary file around is handy would be when you need
to safely create and fill a temp file before it is
atomically moved into place with os.rename(). E.g.
def update_conf(conf_path):
old = open(conf_path)
tmp = tempfile.NamedTemporaryFile(prefix =
os.basename(conf_path), \
dir = os.dirname(conf_path), delete = False)
for l in old:
tmp.write(l.replace('war', 'peace'))
close(old)
close(tmp)
os.link(conf_path, conf_path + ".old")
os.rename(tmp.name, conf_path)
|
|
Date |
User |
Action |
Args |
2007-08-23 15:54:01 | admin | link | issue1537850 messages |
2007-08-23 15:54:01 | admin | create | |
|