PermissionError Errno 13权限被拒绝

时间:2018-06-08 11:00:26

标签: python html permissions

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

    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),但仍然没有。我启用了与所有人的共享,为每个人提供了读/写权限,并且还禁用了#34;只读#34; (以某种方式正在重新启用)。我还将cmd作为管理员运行,但仍然没有进展。任何人都知道如何克服这个问题?

1 个答案:

答案 0 :(得分:2)

您正在尝试打开文件夹进行编写:

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

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

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