如何使用python保存临时jpg文件?

时间:2010-10-26 16:26:48

标签: python temporary-files

嘿我需要保存一个临时的jpg文件,然后将其删除,有没有更好的方法呢? 我测试了tempfile,但看起来不起作用。

1 个答案:

答案 0 :(得分:3)

tempfile确实有用。你尝试了什么?

>>> with tempfile.NamedTemporaryFile(mode="wb") as jpg:
...     jpg.write(b"Hello World!")
...     print jpg.name
...
c:\users\<...>\appdata\local\temp\tmpv7hy__
只要剩下jpg块,

with就会关闭。如果您传入可选参数delete,它将在关闭时删除。