Python permission error when importing a png - python

VsCode gives me this error:
PS D:\leomu\Documents\Python projects\Bots> &
C:/Python/Python39/python.exe "d:/leomu/Documents/Python
projects/Bots/verify.py"
File "d:\leomu\Documents\Python projects\Bots\verify.py", line 4, in <module>
img = Image.open("D:\leomu\Documents\Python projects\Bots\FototsChat") File
"C:\Python\Python39\lib\site-packages\PIL\Image.py", line 2904, in
open
fp = builtins.open(filename, "rb") PermissionError: [Errno 13] Permission denied: 'D:\\leomu\\Documents\\Python
projects\\Bots\\FototsChat'
when running this:
import pytesseract, time
from PIL import Image
img = Image.open("D:\leomu\Documents\Python projects\Bots\FototsChat")
text = pytesseract.image_to_string("img")
print(text)

The problem is that you are trying to load a file which is protected and cant be openened/changed by non admin users. To fix that, it depends what OS you are on
For windows users:
Open the terminal/your IDE as administrator and that should do the trick.
For linux/mac users:
Open run put sudo before your command to run the program.
Edit: i read the question better and realised the path to the image points to either a file without extension (like .png) or a folder (i suspect its a folder).
So if you are trying to open the file 'test.png' from within that folder, just do something like 'D:\leomu\Documents\Python projects\Bots\FototsChat\test.png'

Related

read_pdf FileNotFoundError: [Errno 2] No such file or directory: in Python

I am trying to scrape tables from pdf with read_pdf in python. I am using read_pdf but it doesn't do the job. Also, to mention, I do this in MAC with Jupiter notebook.
This is what I do:
from tabula import read_pdf
file = read_pdf(r'C:\Users\myname\Rprojects\Reports_scraping\data_scraped\icnarc_29052020\icnarc_200529.pdf')
I get this error:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\myname\\Rprojects\\Reports_scraping\\data_scraped\\icnarc_29052020\\icnarc_200529.pdf'
How I can solve this issue?
just to check that the file exist, do you get True when running this:
import os
file_path = r'C:\Users\myname\Rprojects\Reports_scraping\data_scraped\icnarc_29052020\icnarc_200529.pdf'
print( os.path.isfile(file_path))
Edit file_path with wherever is the file(using Python 3).
And did you change "myname" in the path with your actual username... (just in case)
It is preferable to build your paths using os.path.join to make things compatible, on windows it will need to create a root "config.py" file, see
how to get the root folder on windows
#
having discussed with GaB, it seemed that he is using Jupyter notebook on Mac, which explains issues, I saw this link, but can't help more.
Jupyter - import pdf
os.path.join doc
There can be only one possibility, the file is not there, but you have already checked that I assume, if not, Once again check whether the spelling of file is correct. If this doesn't work, then do below trick
Execute the py code in same folder as file, and then use
from tabula import read_pdf
file = read_pdf(r'icnarc_200529.pdf')
Sometimes, this simple method does the trick.

Convert JPG to PNG

I have used the below code to
convert JPG into PNG file:
But when i am running this code from the command line terminal using: python a.py "C:\Users\nishant.gupta2\PycharmProjects\jpgtopngconverter\photo" new
The system is giving me the error:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\nishant.gupta2\\PycharmProjects\\jpgtopngconverter\\photo'
My code is below:
import sys
import os
from PIL import Image
image_folder=sys.argv[1]
output_folder=sys.argv[2]
if not os.path.exists(output_folder):
os.mkdir(output_folder)
for items in os.listdir(image_folder):
im= Image.open(f'{image_folder}')
im.save(f'{output_folder}.png','png')
It looks like this has to do with file permissions rather than your code. Your code is running under a separate user than the image folder you are specifying, depending on admin status you may get permission issues.
You are trying to open a folder here :
im= Image.open(f'{image_folder}')
rather then image. You should specify the path of image.

FileNotFoundError: [Errno 2] No such file or directory: 'gs': 'gs'

Trying to execute this command:
tables = camelot.read_pdf(os.path.join(data_dir,pdf),flavor='lattice')
I'm trying to read a pdf in lattice flavor, but I keep getting this error related to ghostscript which I've already installed:
FileNotFoundError: [Errno 2] No such file or directory: 'gs': 'gs'
Any help is appreciated?
Even if Ghostscript is installed, it seems that is not accessible through python. I suggest you check that you can call gs from the command line as the user running the python script. You can also try to call gs from the python REPL, by calling python from the command line and run the following lines:
import subprocess
subprocess.check_call(['gs', '--version'])
If it is not found try to add the path where the gs binary is located to the PATH environment variable.

WinError 5:Access denied PyTesseract

I know this question has already been answered on this site, however, none of the solutions I looke up the internet seemed to work. Here's what I tried:
Giving all permissions to my python file
Changing PATH variable to point to my tesseract folder
Running IDLE as administrator and then executing the file from there
This error is quite bothering me now and I can't advance any further because of it.
Here's my code if that's going to help:
import pytesseract
import sys
import argparse
try:
import Image
except ImportError:
from PIL import Image
from subprocess import check_output
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR'
c=pytesseract.image_to_string(Image.open('img.png'))
print(c)
Traceback:
Traceback (most recent call last):
File "C:\Users\Hp\Desktop\bot.py", line 12, in <module>
c=pytesseract.image_to_string(Image.open('captcha.png'))
File "C:\Python\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
config=config)
File "C:\Python\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "C:\Python\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Python\lib\subprocess.py", line 992, in _execute_child
startupinfo)
PermissionError: [WinError 5] Accès refusé
I encountered same problem, and I fixed it as 0xc0de said, change the below line:
pytesseract.pytesseract.tesseract_cmd=r"C:\MyApps\Tesseract-ocr\"
to:
pytesseract.pytesseract.tesseract_cmd="C:\\MyApps\\Tesseract-ocr\\tesseract.exe"
I suspect a few things, not sure about any though.
First and the most obvious, the path to Tesseract is not complete. It should be something like:
tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract'
I believe your path points to a directory/folder and not an executable, though only you can confirm that. Let me know if this is incorrect, I see something else too that doesn't seem right at first, but needs more investigation.
Use this to read the tesseract path and also make sure that you have installed the Tesseract- OCR
pytesseract.pytesseract.tesseract_cmd = r'C:\\\Program Files (x86)\\\Tesseract-OCR\\\tesseract.exe'
using always double \\ instead of a single "\"
You need to install tesseract separately. I am providing the link from where you should install
https://github.com/UB-Mannheim/tesseract/wiki
tesseract-ocr-w32-setup-v5.0.0-alpha.20201127.exe (32 bit) and
tesseract-ocr-w64-setup-v5.0.0-alpha.20201127.exe (64 bit) resp.
choose here according to your system config. most of us have 64 bit. so choose that.
Install the file very carefully. I Would suggest doing it in a separate drive other than c.
take the path where you have install the tesseract.exe file. and paste it at pytesseract.pytesseract.tesseract_cmd
look the code...
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'E:/OCR/tesseract_install/tesseract.exe'
img = cv2.imread('E:/OCR/example1.png')
# to see the image below codes are there
cv2.imshow('sampleimage',img)
#enter any key to destroy the image window opened due to previous line code
cv2.waitKey(0)
cv2.destroyAllWindows()
#convert image to text using tesseract
text = pytesseract.image_to_string(img)
print(text)
I had this issues pretty consistent, and I fix it without any access change.
The trick is:
1: You need to install tesseract and remember where is path to tesseract.exe file.
2: Then install pytesseract on exect environment you going to use it using pip install pytesseract
3: Do not add any Path to your System Env variables, it will mess up with everything.
4: CLEARLY define path to image file, or even better if image will be in closest/the same directory where is your python code.
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd=r'C:/Users/your_name/AppData/Local/Programs/Tesseract-OCR/tesseract'
#this path to image
path = r'C:\Users\irina_max\Documents\Python_Scripts\Invoice 1.jpg'
image= Image.open(path)
image
Install the tesseract exe in a directory inside your project. I did the above and it worked fine, without giving Win 5 error.

Permission denied error while writing to a file in Python

I want to create a file and write some integer data to it in python. For example, I have a variable abc = 3 and I am trying to write it to a file (which doesn't exist and I assume python will create it on its own):
fout = open("newfile.dat", "w")
fout.write(abc)
First, will python create a newfile.dat on its own? Secondly, it's giving me this error:
IOError: [Errno 13] Permission denied: 'newfile.dat'
What's wrong here?
Please close the file if its still open on your computer, then try running the python code.
I hope it works
This also happens when you attempt to create a file with the same name as a directory:
import os
conflict = 'conflict'
# Create a directory with a given name
try:
os.makedirs(conflict)
except OSError:
if not os.path.isdir(conflict):
raise
# Attempt to create a file with the same name
file = open(conflict, 'w+')
Result:
IOError: [Errno 13] Permission denied: 'conflict'
I've had the same issue using the cmd (windows command line) like this
C:\Windows\System32> "G:\my folder\myProgram.py"
Where inside the python file something like this
myfile = open('myOutput.txt', 'w')
The error was that when you don't use a full path, python would use your current directory, and because the default directory on cmd is
C:\Windows\System32
that won't work, as it seems to be write-protected and needs permission & confirmation form an administrator
Instead, you should use full paths, for example:
myfile = open('G:\my folder\myOutput.txt', 'w')
Permission denied simply means the system is not having permission to write the file to that folder. Give permissions to the folder using "sudo chmod 777 " from terminal and try to run it. It worked for me.
I write python script with IDLE3.8(python 3.8.0)
I have solved this question:
if the path is
shelve.open('C:\\database.dat')
it will be
PermissionError: [Errno 13] Permission denied: 'C:\\database.dat.dat'.
But when I test to set the path as
shelve.open('E:\\database.dat')
That is OK!!!
Then I test all the drive(such as C,D,F...) on my computer,Only when the Path set in Disk
C:\\
will get the permission denied error.
So I think this is a protect path in windows to avoid python script to change or read files in system Disk(Disk C)
In order to write on a file by using a Python script, you would have to create a text file first.
Example A file such as C:/logs/logs.txt should exist.
Only then the following code works:
logfile=open(r"C:/logs/logs.txt",'w')
So summary.
A text file should exist on the specified location
Make sure you close the file before running the Python script.
To answer your first question: yes, if the file is not there Python will create it.
Secondly, the user (yourself) running the python script doesn't have write privileges to create a file in the directory.
If you are executing the python script via terminal pass --user to provide admin permissions.
Worked for me!
If you are using windows run the file as admin.
If you are executing via cmd, run cmd as admin and execute the python script.
Make sure that you have write permissions for that directory you were trying to create file by properties

Categories

Resources