我怎么能在江中找到tempfile的路径

2024-04-29 06:53:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在这样使用pdftk

pdftktemplate.pdf填写表单/temp/input.fdf output/temp/output.pdf

现在一切正常

但现在我已经生成了临时文件,而不是/temp/input.fdf

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

现在我不知道如何将我的文件作为输入传递给pdftk


Tags: 文件表单closeinputoutputpdfseektempfile
2条回答

myfile.name将获得文件路径。

注意tempfiles在close()之后不存在。从文档中:

 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.

来源:http://docs.python.org/2/library/tempfile.html

你不能用这个名字吗

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

相关问题 更多 >