IOError:[Errno 13]试图在“w”mod中打开隐藏文件时权限被拒绝

2024-05-23 16:20:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想替换隐藏文件的内容,所以我试图以w模式打开它,以便将其删除/截断:

>>> import os
>>> ini_path = '.picasa.ini'
>>> os.path.exists(ini_path)
True
>>> os.access(ini_path, os.W_OK)
True
>>> ini_handle = open(ini_path, 'w')

但这导致了回溯:

IOError: [Errno 13] Permission denied: '.picasa.ini'

但是,我可以使用r+模式实现预期的结果:

>>> ini_handle = open(ini_path, 'r+')
>>> ini_handle.truncate()
>>> ini_handle.write(ini_new)
>>> ini_handle.close()

Q.这两种模式之间有什么区别,一种模式有“权限被拒绝”,而另一种模式工作正常?

更新:我使用Python2.6.6在Win7x64上,目标文件设置了其隐藏属性。当我尝试关闭隐藏属性时,w模式成功。但当我打开它时,它又失败了。

Q.为什么隐藏文件的w模式失败?这是众所周知的行为吗?


Tags: 文件pathimporttrue内容属性accessos
2条回答

以下是详细的区别:

``r'' Open text file for reading. The stream is positioned at the beginning of the file.

``r+'' Open for reading and writing. The stream is positioned at the beginning of the file.

``w'' Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

``w+'' Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

``a'' Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar.

``a+'' Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subse- quent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar.

来自python文档-http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files:-

On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.

因此,如果您使用的是w模式,那么您实际上是在尝试创建一个文件,而您可能没有这样做的权限。r+是合适的选择。

如果您还不知道.picasi.ini存在与否,并且您的windows用户在该目录中具有文件创建权限,并且您希望附加新信息而不是从文件的开头开始(也称为“附加”),则a+将是适当的选择。

文件是否隐藏与此无关。

这就是Win32 API的工作原理。在引擎盖下,Python的open函数正在调用^{}函数,如果失败,它会将Windows错误代码转换为Python IOError

打开模式对应于GENERIC_READ|GENERIC_WRITEdwAccessModeOPEN_EXISTINGdwCreationDispositionw打开模式对应于GENERIC_WRITEdwAccessModeCREATE_ALWAYSdwCreationDisposition

如果您仔细阅读CreateFile文档中的注释,它会说:

If CREATE_ALWAYS and FILE_ATTRIBUTE_NORMAL are specified, CreateFile fails and sets the last error to ERROR_ACCESS_DENIED if the file exists and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_SYSTEM attribute. To avoid the error, specify the same attributes as the existing file.

因此,如果直接从C代码调用CreateFile,解决方案是将FILE_ATTRIBUTE_HIDDEN添加到dwFlagsAndAttributes参数(而不是仅仅FILE_ATTRIBUTE_NORMAL)。但是,由于Python API中没有告诉它传入该标志的选项,您只需使用不同的打开模式或使文件不隐藏来解决它。

相关问题 更多 >