How to fix the permission denied error? (happens in any IDE) - python

I am trying to run a python file. But i got this error.
Traceback (most recent call last):
File "modeltraining.py", line 29, in <module>
sr,audio = read(source + path)
File "C:\Users\RAAM COMPUTERS\Anaconda3\lib\site-packages\scipy\io\wavfile.py", line 233, in read
fid = open(filename, 'rb')
PermissionError: [Errno 13] Permission denied: 'development_set/'

Run Spyder as administrator
Right click --> run as administrator
Or may be you can change the permissions of the directory you want to save to so that all users have read and write permissions.

After some time relaunching Anaconda and Spyder, I got an alert from Avast antivirus about protecting me from a malicious file, the one I was trying to create.
After allowing it, the "[Errno 13] Permission denied" error disappeared.
In my case, it seem the cause of the problem was Avast locking the directory.
numpy.save(array, path) worked fine, but PIL.Image().save(path) was blocked.

I am completely late to the party, but here is a tip for someone who tried everything and it didn't work.
In Spyder go to python->PYTHONPATH manager and add path to the folder with your data there.
Worked for me

I had the permission error when accessing a file on an external card. I guess the error has nothing to do with anaconda, that is just accidentally in the traceback.
Traceback (most recent call last):
File "C:\Users\Admin\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3343, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-219c041de52a>", line 105, in <module>
bs = open(filename, 'rb').read()
PermissionError: [Errno 13] Permission denied: 'D:\\[MYFILEPATH]\\test.bson'
I have checked this error in Spyder and PyCharm, it seems to be independent from the IDE. As the (Windows) solutions here (run as admin, add pythonpath) could not help me, my workaround was to copy the directory to my local disk and work from there.
Later I realised that it is obviously just the one file that is accessed ant that throws the permission that needs to be copied to your local disk, while you can use all your codework on externally.
Example:
Error. Get the permission error by accessing external drive "D:\":
filename = "D:\\test.bson"
# This throws the permission error
bs = open(filename, 'rb').read()
Solution. Avoid the permission error by accessing local drive "C:\":
filename = "C:\\Users\\Admin\\Documents\\test.bson"
# This throws no permission error
bs = open(filename, 'rb').read()
The whole code can now be saved on external "D:\test.py".
It might be the Windows Defender Firewall which was also mentioned when I installed PyCharm (and needed some automatic configurations which also did not solve the issue, but could be linked with it). It is clearly a problem of access rights, the firewall as the cause is quite plausible. Perhaps someone else finds out more about this.

Related

Copying Large Amount of File : Permission Error in Python

This is a simple copy file problem and I came here with my solution. There are some questions in Stackoverflow that are not closed as my problem. That's why I opened this.
Explanation:
Python gives errors while copying files, in my case I tried to copy the image data set to another file(318MB). Python gives these errors in random sequence.
Data Set: Data
Python Version: 3.8.13
IDE: VSCode
Code and Error Message:
for jpgfile in glob.iglob(os.path.join(imageData, "*.jpeg")):
shutil.copy(jpgfile, dst_dir)
C:/Users/user1/Desktop/imageData Traceback (most recent call last):
File "c:\Users\user1\Desktop\copyImageJPEG.py", line 35, in
shutil.copy(jpgfile, dst_dir) File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\shutil.py",
line 418, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks) File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\shutil.py",
line 264, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst: PermissionError: [Errno 13] Permission denied:
'C:/Users/user1/Desktop/copyFolder\person110_virus_207.jpeg'
What I've done till now,
Gave permission access with chmod
Tried to copy , copyFile, copy2 functions
Also this ref.
Wrote full path for both source and destination
Encoded the path for both source and destination
Run the python code as Admin
Also this ref.
r-prefix
Those solutions did not work for my problem. For those who can't find a solution like me, here is another solution:
Just close the copyFolder, don't disrupt it. In my case, I got the error while trying to see the last copied image, so the error popped up 50th, 100th, 150th, etc., depending on my check time.
Somehow the operating system blocks the code while we are using the folder.
I will update this post if I get another solution or the real reason for this disruption.
Edit: #Barmar Windows locks files while they're open in an editor. You get an error if you try to overwrite the locked file.

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.

"No such file or directory" when writing to file with python

I've encountered a very bizarre error when trying to write files with python. As of today, whenever I try and open a file in write mode, python throws the error 'No such file or directory', even if the directory definitely exists. All other python functionality seems to be working, including reading files.
A simple example of the problem, when run using the command line from my documents:
>>> with open('test.csv', 'w') as f:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'test.csv'
I'm using a windows 10 machine, python 3.7, with conda environments specific to projects I'm working on.
I have tried:
Restarting
Creating a clean conda environment (conda create --name test python=3.7)
Running in base without a conda environment
Providing the path as an absolute rather than relative, i.e. C:/Users/<myname>/Documents/test.csv
Trying to run this in other directories
The only success I've had is when running the code with append mode. If I try and create a file as above using mode='a' it throws the error, however if I manually create an empty file, I can append into it.
I really have no idea how to resolve this, other than to completely remove anaconda and python and re-install.
try declare a variable for the file before 'with' statement:
file = open('test.csv', 'w')
with file as f:
# your code here

Django source code won't update on server

I have a Django website running and any updates I make to the source code won't update.
(Reason I'm changing the file is because one line of code is generating an error. What's weird is I commented out this line of code that causes the error, but the code still runs and thus still causes the error. In the django.log it shows that line causing the error still, but it also shows it commented out now. So the error log shows my new source code, but the application itself isn't executing the new code)
I am very new to Django, so I don't really know what's going on here (not my website, I got thrown on this project for work.)
Researching around for this, I have already tried to restart apache:
$ sudo apachectl restart
$ sudo service apache2 restart
and I've also tried to touch the wsgi.py file:
$ touch wsgi.py
and I have even deleted the .pyc file. Nothing has worked and the old line of code is still executing, even though the logs show it commented out.
Not sure where else to check or what else I'm missing.
Whichever service you are using, do a full stop and a full start (i.e., not just restart).
sudo service apache2 stop
sudo service apache2 start
If you are using uwsgi or gunicorn, you will have to do the same for them. Some init scripts when issuing restart do not restart the master worker process which might cause a cached compiled version of your file to still reside in memory (with the incorrect code).
With the help of #2ps I was able to figure out my problem. When I tried to stop Apache, the website was still up.
I realized there's another IP address for the website, so I'm guessing the first one must redirect to the second one?
Either way, I reopened SSH in the other IP address, restarted Apache and the source code updated immediately!
UPDATE:
As per #VidyaSagar request, I'm providing more info as it seems to be a weird fluke with Django. My OP was that a certain line of code was causing an error. I commented out this line, deleted the .pyc file, and restarted Apache. Another error occurred (as expected due to the code). So then I un-commented that line back to how it was previously, again deleted the .pyc and restarted Apache, and the system worked like normal. It seems that Django just wanted me to have a fresh re-compile of the file?
Django version: 1.7.4
Traceback of django.log
ERROR Internal Server Error: /upload/
Traceback (most recent call last):
File "/home/company/app/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/company/app/app/geo_app/views.py", line 306, in upload
shutil.make_archive(kml_dir, 'zip', root_dir=kml_dir)
File "/usr/lib/python2.7/shutil.py", line 521, in make_archive
save_cwd = os.getcwd()
OSError: [Errno 2] No such file or directory
ERROR Internal Server Error: /upload/
Traceback (most recent call last):
File "/home/company/app/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/company/app/app/geo_app/views.py", line 306, in upload
# shutil.make_archive(kml_dir, 'zip', root_dir=kml_dir)
File "/usr/lib/python2.7/shutil.py", line 521, in make_archive
save_cwd = os.getcwd()
OSError: [Errno 2] No such file or directory
For those of you with cPanel, if you go under "Setup Python App" and click "Restart" it should update. Saved me about 5 times.

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