Patches item #1537850, was opened at 2006-08-10 06:57 Message generated for change (Comment added) made by gbrandl You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1537850&group_id=5470
Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Library (Lib) Group: Python 2.5 >Status: Closed >Resolution: Accepted Priority: 5 Private: No Submitted By: Damien Miller (djmdjm) Assigned to: Nobody/Anonymous (nobody) Summary: option to leave tempfile.NamedTemporaryFile around on close Initial Comment: 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) ---------------------------------------------------------------------- >Comment By: Georg Brandl (gbrandl) Date: 2007-03-13 18:32 Message: Logged In: YES user_id=849994 Originator: NO Thanks for the patch, committed with doc changes as rev. 54344. ---------------------------------------------------------------------- Comment By: Damien Miller (djmdjm) Date: 2006-08-26 06:47 Message: Logged In: YES user_id=1359232 Well, that would a) not be an atomic replacement and b) you miss (or would have to reimplement) the mkstemp() like behaviour. ---------------------------------------------------------------------- Comment By: Brett Cannon (bcannon) Date: 2006-08-26 06:40 Message: Logged In: YES user_id=357491 Right, it doesn't create a filesystem file. But that is the point. You work in memory and then write to your final destination as needed. Your code you have pasted in the description does nothing special that requires the use of a temporary file. You can just write into a StringIO object, skip the os.link call, and then just write out to the final file location. ---------------------------------------------------------------------- Comment By: Damien Miller (djmdjm) Date: 2006-08-25 04:27 Message: Logged In: YES user_id=1359232 Here is an diff that includes a regress test ---------------------------------------------------------------------- Comment By: Damien Miller (djmdjm) Date: 2006-08-25 00:22 Message: Logged In: YES user_id=1359232 As far as I can tell, StringIO doesn't actually create a filesystem object that can be manipulated. ---------------------------------------------------------------------- Comment By: Brett Cannon (bcannon) Date: 2006-08-24 23:52 Message: Logged In: YES user_id=357491 Why can't you store into an instance of StringIO instead of a temp file? ---------------------------------------------------------------------- Comment By: Damien Miller (djmdjm) Date: 2006-08-10 06:58 Message: Logged In: YES user_id=1359232 oops, wrong Category: this should be Lib and not Modules ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1537850&group_id=5470 _______________________________________________ Patches mailing list Patches@python.org http://mail.python.org/mailman/listinfo/patches