6

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')
.....
We're All Mad Here
  • 1,544
  • 3
  • 18
  • 46
  • is your file open already in windows I got for this...please try after closing the file/files – Rajarshi Das Dec 27 '15 at 07:03
  • No unfortunately for me it is closed, netherless, i closed and reopened cmd but still nothing. – We're All Mad Here Dec 27 '15 at 07:05
  • Thanks, I remade the way I was reading the file and it worked, though I am pretty sure there are more ways to do this but anyway. I changed `f=open(destPath, 'r')` for `os.path.exists(destPath)` `f=os.access(destPath, os.R_OK)` – We're All Mad Here Dec 27 '15 at 07:11
  • 1
    Not certain if it's what you need, but these questions look similar: http://stackoverflow.com/questions/13215716/ioerror-errno-13-permission-denied-when-trying-to-open-hidden-file-in-w-mod and http://stackoverflow.com/questions/11924981/ioerror-errno-13-permission-denied-while-opening-a-file Other than that, are you certain you have read permission ? Also, if destPath is a directory, why are you trying to open & read it? If it isn't one, why are you passing it to os.walk? – user1245262 Dec 27 '15 at 07:15
  • 2
    `f = open(filename, 'r')` not `destPath` to open – Rajarshi Das Dec 27 '15 at 07:18
  • No destPath not filename, at least that's what worked for me. – We're All Mad Here Dec 27 '15 at 07:23

3 Answers3

13

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

Gary van der Merwe
  • 9,134
  • 3
  • 49
  • 80
0

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

-2

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.

anshu
  • 361
  • 3
  • 3
  • your answer is not relevant to above question, the question is about reading a file and you posting about the "create file". Please read the wiki of how to answer questions: https://stackoverflow.com/help/how-to-answer – n1tk Dec 25 '17 at 05:51