Gerasimos
Gerasimos

Reputation: 319

PermissionError Errno 13 Permission denied

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?

Upvotes: 2

Views: 24567

Answers (1)

BoarGules
BoarGules

Reputation: 16942

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

Upvotes: 2

Related Questions