use*_*652 4 python temporary-files
嘿,我需要临时保存一个jpg文件,然后将其删除,有什么更好的方法吗?我测试了临时文件,但看起来不起作用。
tempfile确实有效。你尝试了什么?
>>> with tempfile.NamedTemporaryFile(mode="wb") as jpg:
... jpg.write(b"Hello World!")
... print jpg.name
...
c:\users\<...>\appdata\local\temp\tmpv7hy__
Run Code Online (Sandbox Code Playgroud)
jpg将在with离开该块后立即关闭。如果您传入可选参数delete,它将在关闭时被删除。