Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, make a tempfile in the same directory as another file?

Tags:

I need to update a file. I read it in and write it out with changes. However, I'd prefer to write to a temporary file and rename it into place.

temp = tempfile.NamedTemporaryFile()
tempname = temp.name
temp.write(new_data)
temp.close()
os.rename(tempname, data_file_name)

The problem is that tempfile.NamedTemporaryFile() makes the temporary file in /tmp which is a different file system. This means os.rename() fails. If I use shlib.move() instead then I don't have the atomic update that mv provides (for files in the same file system, yadda, yadda, etc.)

I know tempfile.NamedTemporaryFile() takes a dir parameter, but data_file_name might be "foo.txt" in which case dir='.'; or data_file_name might be "/path/to/the/data/foo.txt" in which case dir="/path/to/the/data".

What I'd really like is the temp file to be data_file_name + "some random data". This would have the benefit of failing in a way that would leave behind useful clues.

Suggestions?

like image 987
TomOnTime Avatar asked Jan 05 '12 15:01

TomOnTime


2 Answers

You can use:

  • prefix to make the temporary file begin with the same name as the original file.
  • dir to specify where to place the temporary file.
  • os.path.split to split the directory from the filename.

import tempfile
import os
dirname, basename = os.path.split(filename)
temp = tempfile.NamedTemporaryFile(prefix=basename, dir=dirname)
print(temp.name)
like image 178
unutbu Avatar answered Oct 24 '22 05:10

unutbu


You can pass a file location in 'dir' constructor parameter. It works, as you wish.

>>> t = tempfile.NamedTemporaryFile(dir="/Users/rafal")
>>> t.name
'/Users/rafal/tmplo45Js'

Source: http://docs.python.org/library/tempfile.html#tempfile.NamedTemporaryFile

like image 34
Rafał Rawicki Avatar answered Oct 24 '22 07:10

Rafał Rawicki