Permission Denied To Write To My Temporary File

醉酒当歌 提交于 2019-11-27 14:59:48

NamedTemporaryFile actually creates the file for you, there's no need for you to open it for write.

In fact, the Python docs state:

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

That's why you're getting your permission error. What you're probably after is:

f = tempfile.NamedTemporaryFile(mode='w') # open file
temp = f.name                             # get name (if needed)
Vidyesh Ranade

Use the delete parameter as below:

tmpf = NamedTemporaryFile(delete=False)

But then you need to manually delete the temporary file once you are done with it.

tmpf.close()
os.unlink(tmpf.name)

Reference for bug: https://github.com/bravoserver/bravo/issues/111

regards, Vidyesh

Permission was denied because the file is Open during line 2 of your code.

close it with f.close() first then you can start writing on your tempfile

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