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?
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With