PermissionError Errno 13 Permission denied

99封情书 提交于 2019-12-24 08:47:17

问题


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))

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?


回答1:


You are trying to open a folder for writing:

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

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() 


来源:https://stackoverflow.com/questions/50759182/permissionerror-errno-13-permission-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!