Errno13, Permission denied when trying to read file

28.3k Views Asked by At

I have created a small python script. With that I am trying to read a txt file but my access is denied resolving to an no.13 error, here is my code:

import time
import os

destPath = 'C:\Users\PC\Desktop\New folder(13)'
for root, dirs, files in os.walk(destPath):

f=open(destPath, 'r')
.....
3

There are 3 best solutions below

2
Gary van der Merwe On BEST ANSWER

Based on the name, I'm guessing that destPath is a directory, not a file. You can do a os.walk or a os.listdir on the directory, but you can't open it for reading. You can only call open on a file.

Maybe you meant to call open on one or more of the items from files

1
anshu On

I got this issue when trying to create a file in the path -C:/Users/anshu/Documents/Python_files/Test_files . I discovered python couldn't really access the directory that was under the user's name. So, I tried creating the file under the directory - C:/Users/anshu/Desktop . I was able to create files in this directory through python without any issue.

0
Jerry Camacho On

1: I take it you are trying to access a file to get what's inside but don't want to use a direct path and instead want a variable to denote the path. This is why you did the destPath I'm assuming.

From what I've experienced the issue is that you are skipping a simple step. What you have to do is INPUT the location then use os.CHDIR to go to that location. and finally you can use your 'open()'.

From there you can either use open('[direct path]','r') or destPath2 = 'something' then open(destPath2, 'r').

To summarize: You want to get the path then NAVIGATE to the path, then get the 'filename' (can be done sooner or not at all if using a direct path for this), then open the file.

2: You can also try adding an "r" in front of your path. r'[path]' for the raw line in case python is using the "\" for something else.

3: Try deleting the "c:/" and switching the / to \ or vice versa.

That's all I got, hope one of them helps! :-)