42

In python, I am currently experimenting with what I can do with open command. I tried to open a file, and got an error message. Here's my code:

open(r'C:\Users\****\Desktop\File1')

My error message was:

PermissionError: [Errno 13] Permission denied: 'C:\\Users\\****\\Desktop\\File1'

I looked on the website to try and find some answers and I saw a post where somebody mentioned chmod. 1. I'm not sure what this is and 2. I don't know how to use it, and thats why I've come here.

0

6 Answers 6

70

For future searchers, if none of the above worked, for me, python was trying to open a folder as a file.

Check at the location where you try to open the file, if you have a folder with exactly the same name as the file you try to open (the file extension is part of the file name).

1
  • 2
    And that will perhaps also solve the question. It seems as if C:\Users\****\Desktop\File1 is written as a directory path, not with actually as a file path, since there is no file extension. Commented Nov 19, 2020 at 21:53
10

Your user don't have the right permissions to read the file, since you used open() without specifying a mode.

Since you're using Windows, you should read a little more about File and Folder Permissions.

Also, if you want to play with your file permissions, you should right-click it, choose Properties and select Security tab.

Or if you want to be a little more hardcore, you can run your script as admin.

SO Related Questions:

0
7

If nothing worked for you, make sure the file is not open in another program. I was trying to import an xlsx file and Excel was blocking me from doing so.

4

The problem here is your user doesn't have proper rights/permissions to open the file this means that you'd need to grant some administrative privileges to your python ide before you run that command.

As you are a windows user you just need to right click on python ide => select option 'Run as Administrator' and then run your command.

And if you are using the command line to run the codes, do the same open the command prompt with admin rights. Hope it helps

1
  • 8
    Selecting the option Run as Administrator did not seem to work for me.
    – spectre
    Commented Nov 17, 2021 at 6:15
0

If you have this problem in Windows 10, and you know you have premisions on folder (You could write before but it just started to print exception PermissionError recently).. You will need to install Windows updates... I hope someone will help this info.

0

The Reason most of the time is because the file security is set to not modify or write to normal users

To enable Full control to Users

right-click the file, choose Properties and select the Security tab. Then Users then Edit, select Users then

Check Full control

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.