Permission to save a file in Python - python

This code
from PIL import ImageGrab, Image
im = ImageGrab.grab()
im.save(r'D:/MyFilesForProject/Network/screen.jpg')
cause this error
Traceback (most recent call last):
File "G:/PycharmProjects/2.7NetworkExersice/Screenshot.py", line 4, in
<module>
im.save(r'D:\MyFilesForProject\Network\screen.jpg')
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1676, in save
fp = builtins.open(fp, "wb")
IOError: [Errno 13] Permission denied:
'D:\\MyFilesForProject\\Network\\screen.jpg'
I tried to find a solution in the whole internet and I did not find anything working. In order to avoid repetitive advice: yes, I allowed reading and writing the folder in the properties and tried to save it in another place. I really hope for your help. Thank you in advance.

THere is a problem with Python having Permission to write to that drive.
Try to run Python as an Admin:
From commandline:
runas.exe /user:administrator "C:\Python27\python.exe Screenshot.py"
I managed to replicate your error by attempting to save to folder that did not have write permissions.
This is how I fixed the error:
Right click on the folder you wish to save to.
Click properties at the bottom of the list
Go to the security tab
Click the Edit button next to To change permissions, click Edit
make sure the write section has a tick in the Allow column for the user you are logged in as. If this is not a private folder or a security risk you can allow for everyone
One other thing I noted was I had the error from the updated fix
File "C:\Anaconda3\lib\site-packages\PIL\Image.py", line 1725, in save
fp = builtins.open(filename, "w+b")
note the w+b whereas in your traceback you have wb
This could be due to using an outdated version of PIL..
Try uninstalling PIL via PIP and then reinstalling the latest version with
pip install pil
One other difference is that I ran this on Python 3.6 not 2.7 like yourself.

Related

Exception has occurred: PermissionError

I'm trying to make a program with python that will be able to analyse some game event's statistics with OCR and make a summary of a player/group's performance. I'm really new to Python, and I had multiple Python (2 - 3) installed before because I was trying it a little bit years ago. So I uninstalled everything and reinstalled the latest Python 3 to the default/recommended installation (AppData) path.
def ocr_core(img):
text = pytesseract.image_to_string(img)
return text
Error:
Exception has occurred: PermissionError
[WinError 5] Access is denied
File "G:\PythonProjects\NewWorldWarStats.py", line 7, in ocr_core
text = pytesseract.image_to_string(img)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "G:\PythonProjects\NewWorldWarStats.py", line 25, in <module>
print(ocr_core(img))
^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied
I'm trying pytesseract (same permission error with other OCR library) and cv2 to do the task but it gives me permission error no matter if I edit permission to that folder, run the Visual Studio Core as admin. I was also tried to run the project on other drives, nothing worked yet. Any ideas?
Okay, I found out what was the problem. I had to put the whole "tesseract.exe" path, not just its main folder, altho most of the guides only put the folder, not the whole executable path.
So for me, this was the right one:
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\Dany\AppData\Local\Programs\Tesseract-OCR\tesseract.exe'
Thanks for the help.

Denied permissions to download file from Dropbox API in Python

I'm trying to download a CSV file from Dropbox using their library in Python, but I'm having quite the issue trying to get the permissions needed to be able to actually get it into the computer.
The error message that I'm getting in any case is this:
PermissionError: [Errno 13] Permission denied: 'my_directory'
So I've tried the only two solutions I'm finding as I investigate: 1) change the mode of the directory to '0o777' using os.chmod(), and 2) running things as an admin. But even with both, I'm still not getting rights to write the file to my local computer. It might also be worth noting that the name of the file and the directory I'm trying to write it to do not have the same name.
import os
import dropbox
local_dir = 'absolute_path_to_directory'
os.chmod(local_dir, 0o777)
dbx = dropbox.Dropbox('my_token_here')
dbx.files_download_to_file(local_dir, 'path_to_file_in_dropbox')
I would think that running this kind of program as an administrator and having changed the directory permissions, I would have permissions to actually download the file to it. But I'm still getting the PermissionError I noted above.
Any help or insight into what might be causing the issue would be much appreciated.
According to the documentation chmod you can only set the read-only flag all other bits are ignored. However, this might not be the problem.
Looking at the dropbox forum, it looks like you might be missing a '/' along with the filename, and not just the directory.
dbx.files_download_to_file(j, '/'+ j)
According to Greg
You do need to use the full path when specifying the file you want to download. Using files_list_folder and files_list_folder_continue will give you every entry, and you can get the full path from the returned (File)Metadata.path_lower. (I.e., use entry.path_lower instead of '/'+entry.name.)

Error using netCDF4 python module

I am using netCDF4 and python 3.4.
I run the following line of code in order to start writing a new netCDF file that I will be filling with data later in my code following netcdf4 documentation. I however keep getting this error...
File "netCDF4.pyx", line 1466, in netCDF4.Dataset.__init__ (netCDF4.c:19692) RuntimeError: Permission denied
from netCDF4 import Dataset
rootgrp = Dataset('test.nc', 'w', format='NETCDF4')
Any help will be most appreciated.
The "Permission denied" part of the error leads me to believe you don't have permissions to write to the current directory (wherever you are when you run your script).
Check your permissions and/or try giving a full path to put the file in a directory you know you can write to.
dataset.close()
or close ide and delete file 'netCDF4.pyx'
maybe a file exist there, so can't write it.
In my case, this error arises when I am running python by crontab.
The solution is to add following line at the beginning of crontab file.
HDF5_USE_FILE_LOCKING=FALSE

Python - Creating a file works in PyScriptor but not IDE?

When I try to create a new file in PyScriptor using the following code, it works perfectly with no errors but when I use the same code outside of PyScriptor for example in the IDE, it just flashes an error and closes.
file = open("file.txt", "w")
file.write("hello")
file.close()
I cannot get the errors because the error message lasts for less than a second before the Interpreter closes.
Help to figure out why this happens and how to fix it is appreciated.
I am using Python version 2.5.3.0 on windows 7 if this helps.
[EDIT]
Thanks to kirbyfan64sos and JAB for helping me get the errors. It turns out that Python does not have permission to open any file at all (r, w, a etc) yet Pyscriptor has all these permissions so the question still remains: why are they be different and how do i fix them?
I know that it is not the permissions of the file itself otherwise it would not work in Pyscriptor.
[ERROR CODE]
Traceback (most recent call last):
File "IDE File test.py", line 1, in <module>
file = open("file.txt", "w")
IOError: [Errno 13] Permission denied: "file.txt"
Use "w" instead of "W". "W" is an invalid file mode. Also, file.close should be file.close(), as file.close will just return the file object's close method and won't actually call it.
Note that if you start the interpreter from inside the command shell, the shell won't close when an error is raised so you'll be able to see it there. The same applies to using IDLE.
Chances are the permissions of wherever your IDE is running in are not writable by you.
To check:
import os
print(os.path.abspath(os.curdir))
I'm quite certain that you'll find the current path will be different depending on which IDE you run from. Most IDEs have a setting for which path you'll start at. You can also get around that by using os.chdir to change to your home directory (or giving an absolute path to open(), e.g. open('/home/wayne/test.txt', 'w'))

Noob error in Python opening a file

I am placing this code into IDLE:
f = open('/Users/alex/Documents/URM8/health.tdf')
I don't understand why I am unable to open it. I get the error:
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
f = open('/Users/alex/Documents/URM8/health.tdf')
IOError: [Errno 2] No such file or directory: '/Users/alex/Documents/URM8/health.tdf'
Of course usually the problem is filename. I have checked it lots of times and it is correct.
I 'unlocked' the file (I'm using Mac OSX). Also set write access to Everyone in Mac OSX.
Do i need to set permissions in Bash?
Really appreciate someone telling me what I'm doing wrong!
/Users/alex/Documents/URM8/health.tdf cannot be opened because it's not there; the Mac OS UI hides the .txt extension. open('/Users/alex/Documents/URM8/health.tdf.txt') works fine.
The problem is not the permissions. If it were, the error message would be different. Is some component of the path a Mac alias to a directory, rather than a directory? If so, Python won't follow it, and will give that error.
Try individual parts of the pathname to see exactly which directory or file Python can't find. You could do this simply using cd in the shell.
Are you trying to use a relative path? The leading '/' could be a problem.
You don't specifically define the file mode ("r", "w", etc.) in your open call, you may want to reconsider this.
You could try ls -l on the file to get its permissions.
chmod u+rw <FILE> should give you access.

Categories

Resources