Errno 13 Permission Denied in a directory I just created - python

I can create a folder on my Windows desktop:
download_path = os.path.join(os.path.expanduser('~\\Desktop'), (file_downloads')
try:
os.mkdir(download_path)
except Exception as err:
print(err)
But when I try to download a file to it:
def download(file_names):
for file in file_names:
try:
urllib.request.urlretrieve(file, download_path, file.split('/')[-1])
print(f'Successfully downloaded {file}')
except Exception as err:
print(err)
I get this error:
[Errno 13] Permission denied
I have tried other directories where I have changed the permissions so that Everyone has Full Control and I still get the same error. I have also tried running the Python file from the command line as an administrator and I still get the same error.

Related

Why is it saying access denied? [duplicate]

I am trying to call a python program with subprocess, but I get a permission error. I tried running PyCharm as an admin, but it doesn't help.
My code:
answer = subprocess.check_output("../folder python program %s %s" %(valueA, valueB), encoding = 'utf8')
The error:
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/a/b/b_resolution.py", line 35, in <module>
answer = subprocess.check_output("../folder python program %s %s" %(valueA, valueB), encoding = 'utf8')
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 376, in check_output
**kwargs).stdout
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 453, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 1155, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access Denied
Does someone know how I can fix this permission error?
Although it doesn't answer the original question, this PermissionError also arises if you (accidentally) try to run a directory, instead of a file.
For example, any of these will raise the PermissionError: [WinError 5] Access is denied:
subprocess.check_output('.')
subprocess.run('.')
where '.' represents the path to the current directory, as a minimal example.
On the other hand, if you try to run a non-existent file, you'll get a more useful FileNotFoundError: [WinError 2] The system cannot find the file specified.
Tested with python 3.10.6 on Windows and Ubuntu. On Ubuntu the examples above raise a PermissionError: [Errno 13] Permission denied.
Check the file permissions for your current user.
Right click on the file and in security you can see file permissions for users.
If you haven't permission to read file, Advanced > Select a principal then check this doc.
I fixed the problem by myself the python command comes before the path.
Like this:
answer = subprocess.check_output("python ../folder program %s %s" %(valueA, valueB), encoding = 'utf8')
But I had the problem that it says:
can't find '__main__' module in '../pydig'
Solved that aswell with writing the program name included in the path:
answer = subprocess.check_output("python ../folder/program %s %s" %(valueA, valueB), encoding = 'utf8')
close file explorer...
dumb but if you have the folder open in the explorer and you're trying to do anything to the folders/files you'll get this error
Don't run in Visual Studio 🤣. It happened to me just now.

Get more info out of a Python3 OSError

I am using Python3 on Ubuntu with the HID module python3-hid.
If I use it to open a device, I simply get back:
OSError: open failed
Which prompted me to handle the exception, and see if I can get more information out of it:
try:
h.open(vend, prod)
except OSError as error :
print(error)
print(error.strerror)
print(error.filename)
sys.exit(1)
To my surprise, no further information was available, as both strerror and filename were None.
open failed
None
None
NOTE: My open() succeeds when I run as root, so I know this is a permission thing. But why can't this OSError tell me that it is permission related?
How do I get all the details (specific reason of failure, and filename involved) out of an OSError?
UPDATE: The traceback leads to hid.pyx file:
Traceback (most recent call last):
File "/tmp/./tst.py", line 30, in <module>
h.open(vend, prod)
File "hid.pyx", line 66, in hid.device.open
I am not sure why my system can't find the hid.pyx file.
$ dpkg -S hid.pyx
dpkg-query: no path found matching pattern *hid.pyx*
Checking the upstream package from Ubuntu... it turns out that python3-hid is part of the software for a crypto coin wallet? Strange. I think I will skip using this module, and maybe write directly in C, using libhidapi-hidraw which this python module also uses.

How to get the win32 handle of a directory in Python?

You can get the win32 handle of a file in Python by:
file = CreateFile("C:\\File.txt")
handle = str(msvcrt.get_osfhandle(file.fileno()))
file.close()
However to do this you need to make a file object, which cannot be a directory. For example,
dir = CreateFile("C:\\Directory")
handle = str(msvcrt.get_osfhandle(file.fileno()))
dir.close()
This throws an error because 'C:\Directory' is a directory:
PermissionError: [Errno 13] Permission denied: 'C:\\Directory'
See: PermissionError Errno 13 Permission denied
How could you do this or something like this for a directory?

Getting WinError 10002 error after freezing my code with pyinstaller

Update: This seems to be connected specifically to the an issue with pyinstaller freezing the socket package. Running the following code is fine in python, but after freezing to an exe with pyinstaller it gives the OSError.
import socket
try:
socket.socket()
except OSError as e:
print(e)
Original text:
I'm currently trying to build a python script that queries a mySQL database using mysql.connector. Running the code in python works fine, but when I freeze it using pyinstaller and run the exe I get an OSError: [WinError 10022] An invalid argument was supplied when it tries to run the mysql.connector.connect function.
My code is below:
import mysql.connector
def test(user, password, ip, query):
con = mysql.connector.connect(user=user,
password=password,
host=ip)
cursor = con.cursor()
cursor.execute(query)
rows = cursor.fetchall()
cursor.close()
con.close()
return rows
The full error text is:
Traceback (most recent call last):
File "site-packages\mysql\connector\network.py", line 507, in open_connection
File "socket.py", line 134 in __init__
OSError: [WinError 10022] An invalid argument was supplied
Looking around, the only mysql.connector questions related to pyinstaller I can find are due to import errors while the WinError 10022 questions seem to be about other packages.

IDLE on windows error PermissionError: [Errno 13] Permission denied:

I am using IDLE on windows , when i run this below code i get error .
mut = 'F:\Perl\python\Examples'
file_name = open (mut,'r')
Traceback (most recent call last):
File "", line 1, in
file_name = open (mut,'r')
PermissionErroenter code herer: [Errno 13] Permission denied: 'F:\Perl\python\Examples'
'F:\Perl\python\Examples' is the path where my 'mut' file is located.
Please assists i am confused ?
'/' can be used on Windows quite happily, and is simpler.
Looks like Examples is a directory/folder - it should be a filename. That is why you are getting the error.
Note that open() returns a file handle, not a file name. What are you expecting this code to do?
Are you perhaps looking for os.walk() or os.listdir()?

Categories