【发布时间】:2018-07-03 02:22:43
【问题描述】:
我有一个访问某些文件的 Python 脚本,例如,一行代码将一些文本附加到 .txt 文件中。
它可以在正常的 cmd.exe 中正确运行,无论是否具有管理员访问权限。但是在使用 Anaconda Prompt 或从 Anaconda Navigator 打开的 cmd.exe 时,它会在 open('path/to/file', 'a+') 行中提供 PermissionError: [Errno 13] Permission denied:。
我正在为 Anaconda 的 python 无法访问文件的原因寻找解决方案。使用 logging 模块中的函数 handlers.TimedRotatingFileHandler() 的另一行代码也会引发相同的错误。
一些可能有用的信息:
- 我有两个Anaconda环境,一个是python 2.7,一个是python 3.6.6,都报错;
- 我已安装 Anaconda 3,但未将 anaconda 添加到我的 PATH,因为安装程序中建议使用它;
- 我的PATH的python,在非anaconda cmd.exe中运行的版本也是3.6.6;
- 我使用的是 Windows 10。
编辑:
好的,所以对于我要完成的工作似乎有些困惑。我正在递归地使用文件夹中的每个 .jpg 文件制作一个文本文件,然后拆分文件名。对于找到的每个文件,文本文件应该有:“path/to/XY_file_XY.jpg 文件”。这些分割代表我正在训练的神经网络的图像标签。
这是我创建此文本文件的代码:
import glob
class DBSetter(object):
def __init__(self, rootPath = 'C:\\Users\\Victor.Lundgren\\Google Drive\\Mestrado\\VC\\Projeto\\data\\mnt'):
self.path = rootPath
return
def setDB(self, extension = '.jpg', splitFileName = True, splitDilimiter = '_', splitPosition = 1):
query = self.path+'\\**\\*'+extension
print(query)
fileList = glob.glob(query, recursive = True)
sampleFileText = ''
for file in fileList:
word = file.split('\\')[-1]
if splitFileName:
word = word.split(splitDilimiter)[splitPosition]
sampleFileText += file+' '+word+'\n'
print('Sample File Text created.')
sampleFile = open(self.path+'\\sample.txt', 'a+')
sampleFile.write(sampleFileText)
sampleFile.close()
return
例如,让我们在 main.py 中创建一个 DBSetter 对象,并在简单的 cmd.exe 中使用默认 Python (3.6.6) 和 Anaconda 的 Python (3.6.6) 测试 ir在 Anaconda 提示中:
from utils import database_setter
setter = database_setter.DBSetter('C:\\Users\\Victor.Lundgren\\Pictures\\Trabalho\\ASA') #choosing this folder for this example because it has few files in it.
setter.setDB(extension = '.png', splitFileName = False)
这里我们在一个简单的 cmd.exe 调用 main.py:
> Microsoft Windows [versão 10.0.16299.492] (c) 2017 Microsoft
> Corporation. Todos os direitos reservados. Clink v0.4.9 [git:2fd2c2]
> Copyright (c) 2012-2016 Martin Ridgers http://mridgers.github.io/clink
>
>
> C:\Users\Victor.Lundgren>cd C:\Users\Victor.Lundgren\Google Drive\Mestrado\VC\Projeto
>
> C:\Users\Victor.Lundgren\Google Drive\Mestrado\VC\Projeto>Python
> __main__.py C:\Users\Victor.Lundgren\Pictures\Trabalho\ASA\**\*.png Sample File Text created.
>
> C:\Users\Victor.Lundgren\Google Drive\Mestrado\VC\Projeto>
现在从 windows 菜单执行 Anaconda Promp 并使用我的 Python 3.6.6 env 运行相同的东西:
> Clink v0.4.9 [git:2fd2c2] Copyright (c) 2012-2016 Martin Ridgers
> http://mridgers.github.io/clink
>
>
> (base) C:\Users\Victor.Lundgren>cd C:\Users\Victor.Lundgren\Google Drive\Mestrado\VC\Projeto
>
> (base) C:\Users\Victor.Lundgren\Google Drive\Mestrado\VC\Projeto>activate Py3
>
> (Py3) C:\Users\Victor.Lundgren\Google Drive\Mestrado\VC\Projeto>Python
> __main__.py C:\Users\Victor.Lundgren\Pictures\Trabalho\ASA\**\*.png Sample File Text created. Traceback (most recent call last): File
> "__main__.py", line 6, in <module>
> setter.setDB(extension = '.png', splitFileName = False) File "C:\Users\Victor.Lundgren\Google Drive\Mestrado\VC\Projeto\utils\database_setter.py", line 19, in setDB
> sampleFile = open(self.path+'\\sample.txt', 'a+') PermissionError: [Errno 13] Permission denied:
> 'C:\\Users\\Victor.Lundgren\\Pictures\\Trabalho\\ASA\\sample.txt'
>
> (Py3) C:\Users\Victor.Lundgren\Google Drive\Mestrado\VC\Projeto>
【问题讨论】:
-
尝试打开目录也会在 Windows 中引发权限错误,因此首先要排除传递给
open的不正确路径。如果脚本错误地假设进程工作目录始终是脚本目录,我们还必须排除相对路径的问题。它们本质上没有任何关系,只是有时相同,具体取决于脚本的运行方式。 -
有效点,我正在传递一个打开的绝对路径('C:\\Users\\Victor.Lundgren\\Pictures\\Trabalho'),正如我在问题中指出的相同脚本使用默认python正确运行,所以路径存在。
-
@eryksun 我贴错了路径,正确的是'C:\\Users\\Victor.Lundgren\\Pictures\\Trabalho\\sample.txt'。该文件在默认python中正确写入/创建,因此路径不是问题。
-
@eryksun 正如我在问题中提到的,我的默认 Python 版本是 3.6.6。我有一个带有 Python 3.6.6 的 Anaconda 环境。默认的 python 3.6.6 运行它,而 Anaconda python 3.6.6 不运行。这让我觉得它与脚本无关,而是与 Anaconda 的 python 或 anaconda 本身由于某种原因没有获得许可。
标签: windows python-3.x anaconda