Rename python tempfile

python

Solution

You can access the filename via `f.name`. However, unless you use `delete=False` python will (try to) delete the temporary file automatically as soon as it is closed. Disabling auto deletion will keep the tempfile even if you do not save it - so that's not such a good idea.

The best way is copying the file and letting python delete the temporary one when it's closed:

import shutil
shutil.copy(f.name, 'new-name')

Problem

What is the way to rename the following tempfile ``` pdf = render_me_some_pdf() #PDF RENDER f = tempfile.NamedTemporaryFile() f.write(pdf) f.flush() ``` I read somethings about os.rename but I don't really now how to apply it

Original source

Related problems