Python 写入我的临时文件的权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23212435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:30:29  来源:igfitidea点击:

Permission Denied To Write To My Temporary File

pythontemporary-files

提问by Mack

I am attempting to create and write to a temporary file on Windows OS using Python. I have used the Python module tempfileto create a temporary file.

我正在尝试使用 Python 在 Windows 操作系统上创建和写入临时文件。我使用 Python 模块tempfile创建了一个临时文件。

But when I go to write that temporary file I get an error Permission Denied. Am I not allowed to write to temporary files?! Am I doing something wrong? If I want to create and write to a temporary file how should should I do it in Python? I want to create a temporary file in the temp directory for security purposes and not locally (in the dir the .exe is executing).

但是当我去写那个临时文件时,我得到了一个错误Permission Denied。我不允许写入临时文件吗?!难道我做错了什么?如果我想创建并写入一个临时文件,我应该如何在 Python 中做到这一点?为了安全起见,我想在临时目录中创建一个临时文件,而不是在本地(在 .exe 正在执行的目录中)。

IOError: [Errno 13] Permission denied: 'c:\\users\\blah~1\\appdata\\local\\temp\\tmpiwz8qw'

IOError: [Errno 13] Permission denied: 'c:\\users\\blah~1\\appdata\\local\\temp\\tmpiwz8qw'

temp = tempfile.NamedTemporaryFile().name
f = open(temp, 'w') # error occurs on this line

采纳答案by paxdiablo

NamedTemporaryFileactually creates and opensthe file for you, there's no need for you to open it again for writing. In fact, the Python docsstate:

NamedTemporaryFile实际上为您创建并打开文件,您无需再次打开它进行写入。实际上,Python 文档指出:

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).

在命名的临时文件仍处于打开状态时,是否可以使用该名称再次打开该文件,因平台而异(在 Unix 上可以如此使用;在 Windows NT 或更高版本上则不能)。

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)

回答by 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

错误参考:https: //github.com/bravoserver/bravo/issues/111

regards, Vidyesh

问候, 维迪耶什

回答by Richie Zubiri

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

权限被拒绝,因为文件在代码的第 2 行中处于打开状态。

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

先用 f.close() 关闭它,然后你就可以开始在你的临时文件上写了

回答by Erik Aronesty

Consider using os.path.join(tempfile.gettempdir(), os.urandom(24).hex())instead. It's reliable, cross-platform, and the only caveat is that it doesn't work on FAT partitions.

考虑os.path.join(tempfile.gettempdir(), os.urandom(24).hex())改用。它是可靠的、跨平台的,唯一需要注意的是它不适用于 FAT 分区。

NamedTemporaryFile has a number of issues, not the least of which is that it can fail to create files because of a permission error, fail to detect the permission error, and then loop millions of times, hanging your program and your filesystem.

NamedTemporaryFile 有很多问题,其中最重要的是它可能会因为权限错误而无法创建文件,无法检测权限错误,然后循环数百万次,挂起您的程序和文件系统。