0

A python script downloads 2 tables from a database, does some calculations and creates a new one, and uploads it. There are intermediate files involved and to store all the files I create a folder called “temp” in the current working directory. Before the program exits it deletes temp. This doesn’t work very well because

  1. If there already is a folder named temp it will be deleted
  2. If the program crashes before the last step of deleting temp, the next time the program runs it crashes because files with the same name exist from the previous execution.

I imagine this is a relatively common scenario. How is it normally handled?

Celeritas
  • 14,489
  • 36
  • 113
  • 194
  • 1
    To address 1, simply name your folder something more specific than just `temp`. You may also want to add a check to see if the files already exist and maybe overwrite/delete them. – Ben Longo Jun 26 '15 at 23:48

1 Answers1

2

there is a tempfile module just for this

import tempfile
new_dir = tempfile.mkdtemp() #makes a new temp folder guaranteed to be unique(in the os-specific temp location)
...
os.remove(new_dir)

really I dont think you even need to remove the new_dir as it will automagically be gone on next reboot usually(*i think)

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • use try/finally or `atexit` handler to call `os.remove()`. You could use `dir` parameter, to create the temporary directory in the current directory. – jfs Jun 27 '15 at 03:16