我有一个Python脚本,它访问一些文件,例如,一行代码将一些文本附加到一个.txt文件中。它在正常的cmd.exe中正确运行,有或没有Admnistrator访问权限。但是,在使用Anaconda提示符或从Anaconda打开的cmd.exe时,它在open('path/to/file', 'a+')行中给出了一个PermissionError: [Errno 13] Permission denied:。
我正在寻找一个解决方案,解释为什么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例如,让我们在一个DBSetter主.py中创建一个对象,并使用默认的cmd.exe (3.6.6)和Anaconda的Python (3.6.6)在Anaconda提示符中测试ir:
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中记录了cmd历史,它调用了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,并使用我的Python3.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>发布于 2018-07-04 23:48:13
经过多次尝试重新安装Anaconda,我发现了这个问题。我的防病毒软件(Bitdefender)完全阻塞了Anaconda安装的Python版本,出于某种原因,没有显示任何阻止它的通知,删除该块使它正确运行。
https://stackoverflow.com/questions/51145836
复制相似问题