Quick way to save a python TempFile?

南楼画角 提交于 2019-12-10 17:23:19

问题


I am using tempfile.TemporaryFile for some intermediate processing. The program nicely removes the temp file for me when it closes, which is precisely the behaviour that I want the majority of the time. But is there a way to save the file for examination in the event of a (trapped) error or other abnormal circumstance?


回答1:


Seek to the beginning and use shutil.copyfileobj() to copy the data to a file you create.




回答2:


Use tempfile.mkstemp(). According to the documentation:

Unlike TemporaryFile(), the user of mkstemp() is responsible for deleting the temporary file when done with it.

Read more here.



来源:https://stackoverflow.com/questions/10128455/quick-way-to-save-a-python-tempfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!