Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
temporary file names
#2
It's a great idea. I suggest a context manager and a 'commit' keyword argument to the .close() method in order to have code such as
with skap_open("critical.cfg", "w") as ofile:
    ofile.write("frac = {}\n".format(x / y))
    ofile.write("more data\n")
    ofile.close(commit=True)
If we reach the ofile.close(commit=True) statement, critical.cfg is written. If we exit through an exception or some other way, the temporary file is destroyed but critical.cfg is left untouched. The commit keyword defaults to False, so that exiting the context defaults to not overwriting 'critical.cfg'.
Reply


Messages In This Thread
temporary file names - by Skaperen - Dec-31-2018, 07:09 PM
RE: temporary file names - by Gribouillis - Dec-31-2018, 11:02 PM
RE: temporary file names - by Skaperen - Dec-31-2018, 11:57 PM
RE: temporary file names - by Gribouillis - Jan-01-2019, 10:00 AM
RE: temporary file names - by Skaperen - Jan-01-2019, 08:44 PM
RE: temporary file names - by Gribouillis - Jan-02-2019, 09:14 AM
RE: temporary file names - by Skaperen - Jan-02-2019, 07:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  rename same file names in different directories elnk 0 741 Nov-04-2022, 05:23 PM
Last Post: elnk
  Saving Excel workbook file with dataframe names Biplab1985 0 2,061 Jun-07-2020, 12:25 PM
Last Post: Biplab1985
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 5,772 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  splitstring file names a by hyphen steve22020 3 3,326 Oct-30-2019, 05:39 PM
Last Post: steve22020
  Is there a more effecient way to do this ? File Names sumncguy 2 2,121 Jul-11-2019, 12:47 PM
Last Post: DeaD_EyE
  How to combine file names into a list from multiple directories? python_newbie09 3 5,283 Jul-09-2019, 07:38 PM
Last Post: python_newbie09
  write each line of a text file into separate text files and save with different names Shameendra 3 2,837 Feb-20-2019, 09:07 AM
Last Post: buran
  Reading file names CertifiedPengu 1 2,303 Jan-07-2019, 06:54 PM
Last Post: Gribouillis
  how to increment all file names in a folder SoulsKeeper 5 6,124 Sep-10-2018, 07:59 PM
Last Post: Skaperen
  dealing with spaces in file names AceScottie 5 75,669 Jun-02-2018, 01:06 PM
Last Post: AceScottie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020