I'm getting this error :
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1538, in __call__
return self.func(*args)
File "C:/Users/Marc/Documents/Programmation/Python/Llamachat/Llamachat/Llamachat.py", line 32, in download
with open(place_to_save, 'wb') as file:
PermissionError: [Errno 13] Permission denied: '/goodbye.txt'
When running this :
def download():
# get selected line index
index = films_list.curselection()[0]
# get the line's text
selected_text = films_list.get(index)
directory = filedialog.askdirectory(parent=root,
title="Choose where to save your movie")
place_to_save = directory + '/' + selected_text
print(directory, selected_text, place_to_save)
with open(place_to_save, 'wb') as file:
connect.retrbinary('RETR ' + selected_text, file.write)
tk.messagebox.showwarning('File downloaded',
'Your movie has been successfully downloaded!'
'\nAnd saved where you asked us to save it!!')
Can someone tell me what I am doing wrong ? Thanks
Specs : Python 3.4.4 x86 Windows 10 x64
EDIT
I am seeing a bit of activity on my answer so I decided to improve it a bit for those with this issue still
There are basically three main methods of achieving administrator execution privileges on Windows.
- Running as admin from
cmd.exe - Creating a shortcut to execute the file with elevated privileges
- Changing the permissions on the
pythonexecutable (Not recommended)
1) Running cmd.exe as and admin
Since in Windows there is no sudo command you have to run the terminal (cmd.exe) as an administrator to achieve to level of permissions equivalent to sudo. You can do this two ways:
Manually
- Find
cmd.exeinC:\Windows\system32 - Right-click on it
- Select
Run as Administrator - It will then open the command prompt in the directory
C:\Windows\system32 - Travel to your project directory
- Run your program
- Find
Via key shortcuts
- Press the windows key (between
altandctrlusually) +X. - A small pop-up list containing various administrator tasks will appear.
- Select
Command Prompt (Admin) - Travel to your project directory
- Run your program
- Press the windows key (between
By doing that you are running as Admin so this problem should not persist
2) Creating shortcut with elevated privileges
- Create a shortcut for
python.exe - Righ-click the shortcut and select
Properties - Change the shortcut target into something like
"C:\path_to\python.exe" C:\path_to\your_script.py" - Click "advanced" in the property panel of the shortcut, and click the option "run as administrator"
Answer contributed by delphifirst in this question
3) Changing the permissions on the python executable (Not recommended)
This is a possibility but I highly discourage you from doing so.
It just involves finding the python executable and setting it to run as administrator every time. Can and probably will cause problems with things like file creation (they will be admin only) or possibly modules that require NOT being an admin to run.
Change the permissions of the directory you want to save to so that all users have read and write permissions.
THis happened to me on a Windows machine using Pycharm.
The fix: Right-click your PyCharm application and run it as administrator.
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 command line to run the codes, do the same open the command prompt with admin rights.
Make sure the file you are trying to write is closed first.
The problem could be in the path of the file you want to open. Try and print the path and see if it is fine I had a similar problem
def scrap(soup,filenm):
htm=(soup.prettify().replace("https://","")).replace("http://","")
if ".php" in filenm or ".aspx" in filenm or ".jsp" in filenm:
filenm=filenm.split("?")[0]
filenm=("{}.html").format(filenm)
print("Converted a file into html that was not compatible")
if ".aspx" in htm:
htm=htm.replace(".aspx",".aspx.html")
print("[process]...conversion fron aspx")
if ".jsp" in htm:
htm=htm.replace(".jsp",".jsp.html")
print("[process]..conversion from jsp")
if ".php" in htm:
htm=htm.replace(".php",".php.html")
print("[process]..conversion from php")
output=open("data/"+filenm,"w",encoding="utf-8")
output.write(htm)
output.close()
print("{} bits of data written".format(len(htm)))
but after adding this code:
nofilenametxt=filenm.split('/')
nofilenametxt=nofilenametxt[len(nofilenametxt)-1]
if (len(nofilenametxt)==0):
filenm=("{}index.html").format(filenm)
It Worked perfectly
I faced a similar problem. I am using Anaconda on windows and I resolved it as follows: 1) search for "Anaconda prompt" from the start menu 2) Right click and select "Run as administrator" 3) The follow the installation steps...
This takes care of the permission issues
Double check and make sure the file you are trying to write to is not open or some program in the background is not holding on to this file/data. This was the problem for me.
PermissionError: [Errno 13] Permission denied:
I faced above error while working on excel and my excel was open then i closed & tried to run my code. It worked for me.
Your File must be open. Close your file and run the code again, it should work.
来源:https://stackoverflow.com/questions/36434764/permissionerror-errno-13-permission-denied