我有一个访问某些文件的Python脚本,例如,一行代码将一些文本附加到.txt文件中。
它可以在具有或没有Admnistrator访问权限的常规cmd.exe中正常运行。但是,当使用Anaconda Prompt或从Anaconda Navigator打开的cmd.exe时,它在PermissionError: [Errno 13] Permission denied:
行中给出了open('path/to/file', 'a+')
。
我正在寻找一种解决方案,以解释为什么Anaconda的python无法访问文件。使用来自日志记录模块的函数handlers.TimedRotatingFileHandler()
的另一行代码也会引发相同的错误。
一些有用的信息:
好的,所以对于我要完成的工作似乎有些困惑。我正在用递归方式将文件夹中的每个.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历史记录在一个简单的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>
答案 0 :(得分:1)
在多次尝试重新安装Anaconda之后,我发现了问题。我的防病毒软件(Bitdefender)专门阻止了Anaconda安装的Python版本,由于某种原因,它没有显示任何有关被阻止的通知,因此删除了阻止使其正常运行。