Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i find the path of tempfile in django

Tags:

python

django

I am using pdftk like this

pdftk template.pdf fill_form /temp/input.fdf output /temp/output.pdf

Now this is working fine

But now i have generated the temporary file instead of /temp/input.fdf with this

myfile = tempfile.NamedTemporaryFile()
myfile.write(fdf)
myfile.seek(0)
myfile.close()

Now i don't know how can i pass myfile as input to the pdftk

like image 550
Mirage Avatar asked Dec 05 '12 06:12

Mirage


2 Answers

myfile.name will get you the file path.

Note that tempfiles do not exist after close(). From the docs:

 tempfile.TemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, 
    prefix='tmp'[, dir=None]]]]])

Return a file-like object that can be used as a temporary storage area. The file is created using mkstemp(). It will be destroyed as soon as it is closed (including an implicit close when the object is garbage collected). Under Unix, the directory entry for the file is removed immediately after the file is created. Other platforms do not support this; your code should not rely on a temporary file created using this function having or not having a visible name in the file system.

Source: http://docs.python.org/2/library/tempfile.html

like image 82
Yuval Adam Avatar answered Nov 15 '22 16:11

Yuval Adam


Can't you get the name using

myfile = tempfile.NamedTemporaryFile()
myfile.write(fdf)
myfile.seek(0)
myfile.close()
print(myfile.name)
like image 33
Ehtesh Choudhury Avatar answered Nov 15 '22 15:11

Ehtesh Choudhury