How to get the win32 handle of a directory in Python? - 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?

Related

Python - [Errno 13] Permission denied. Not able to open the file

I'm getting IOError: [Errno 13] Permission denied and I don't know what is wrong wit this code.
I'm trying to read a file given an absolute path
with open(r"E:\Paper\codedata.tsv", 'r+') as temp_f:
Error:
Traceback (most recent call last):
File "E:\Paper\code.py", line 18, in <module>
with open(r"E:\Paper\codedata.tsv", 'r') as temp_f:
What I have tried so far:
Changed file permissions. I am using Windows 10
Windows is upto Date
Working in Anaconda spyder. Tried opening spyder in admin and running the code
Changed directory of data to different path
I am using Python 3.

What is the reason for the error ' PermissionError: [Errno 13] Permission denied ' when running the django server

When I try to run the Django server, the server runs properly, but I also get the following error and my developing website does not load properly. What could be the problem?
File "C:\Users\UserName\AppData\Local\Programs\Python\Python38-32\lib\wsgiref\handlers.py", line 183, in finish_response
for data in self.result:
File "C:\Users\UserName\AppData\Local\Programs\Python\Python38-32\lib\wsgiref\util.py", line 37, in __next__
data = self.filelike.read(self.blksize)
PermissionError: [Errno 13] Permission denied
There may be few reasons for that
Literally your account doesn't have proper permission to access it
Where the folder described doesn't exist
Where a folder is described instead of file
So I think you can find the reason if check above items

Error when reading pickle file - permission error

I'm trying to read a pickle file.
df= pd.read_pickle(r'C:\\Users\\Downloads\\pickle_file')
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Downloads\\pickle_file'
I have no other instances of python running. It's my first time importing the pickle file. What causes this issue?

Errno 13 Permission Denied in a directory I just created

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.

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