首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Anaconda提示符中访问文件时拒绝权限

在Anaconda提示符中访问文件时拒绝权限
EN

Stack Overflow用户
提问于 2018-07-03 02:22:43
回答 1查看 4.5K关注 0票数 3

我有一个Python脚本,它访问一些文件,例如,一行代码将一些文本附加到一个.txt文件中。它在正常的cmd.exe中正确运行,有或没有Admnistrator访问权限。但是,在使用Anaconda提示符或从Anaconda打开的cmd.exe时,它在open('path/to/file', 'a+')行中给出了一个PermissionError: [Errno 13] Permission denied:

我正在寻找一个解决方案,解释为什么Anaconda的python不能访问文件。使用来自日志模块的函数handlers.TimedRotatingFileHandler()的另一行代码也会引发相同的错误。

一些可能有用的信息:

  • 我有两个Anaconda环境,一个带有python 2.7,另一个有python 3.6.6,都抛出了错误;
  • 我已经安装了Anaconda 3而没有添加anaconda到我的路径,因为它是在安装程序中推荐的;
  • 我的路径的python,在非anaconda cmd.exe中工作的版本,也是3.6.6;
  • 我使用Windows 10。

编辑:

好吧,看来我想要做的事情有点混乱。我使用文件夹中的每个.jpg文件递归地创建一个文本文件,然后是文件名的拆分。对于找到的每个文件,文本文件应该有:"path/to/XY_ file _XY.jpg文件“。这些分裂代表了我正在训练的神经网络图像的标签。

下面是我创建这个文本文件的代码:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
> 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运行相同的内容:

代码语言:javascript
运行
复制
> 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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-04 23:48:13

经过多次尝试重新安装Anaconda,我发现了这个问题。我的防病毒软件(Bitdefender)完全阻塞了Anaconda安装的Python版本,出于某种原因,没有显示任何阻止它的通知,删除该块使它正确运行。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51145836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档