PermissionError Errno 13权限被拒绝 [英] PermissionError Errno 13 Permission denied

查看:1263
本文介绍了PermissionError Errno 13权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python读取包含html文件的目录.我正在使用的代码是这样:

I am trying to read a directory which contains html files with python. The code I am using is this:

    import os
f = open(r"C:\Users\Grty\Desktop\de", "w+")
for filename in os.listdir(os.getcwd()):
  content = f.read()
  print (filename, len(content))

问题是我无法访问目录.我尝试过其他位置,但问题仍然存在.我还做了相对的chmod 777(使用Windows 10),却一无所获.我启用了与所有人共享的权限,为所有人提供了读/写权限,并且还禁用了只读"功能(通过某种方式本身已被重新启用).我也已经以管理员身份运行cmd,但仍然没有任何进展.任何人都知道如何克服这一点?

The problem is I cant access the directory. I have tried different locations but the problem persists. I have also done the relative chmod 777 (Using windows 10) and still nothing. I enabled sharing with everyone, giving read/write permissions to everyone and also disabled the "read only" (which somehow is being re-enabled itself). I have also run the cmd as an admin and still no progress. Anyone got an idea of how to overcome this?

推荐答案

您正在尝试打开一个用于写入的文件夹:

You are trying to open a folder for writing:

f = open(r"C:\Users\Grty\Desktop\de", "w+")

但这是一个文件夹,即使在"r"模式下也无法使用open()打开,因为它不是文件,并且如果尝试这样做,Windows会说访问被拒绝.当您获得每个filename时,请打开它:

But this is a folder, which can't be opened using open() even in "r" mode, because it isn't a file, and if you try, Windows will say access denied. As you get each filename, open that:

for filename in os.listdir(os.getcwd()):
    with open(filename) as f:
        content = f.read() 

这篇关于PermissionError Errno 13权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆