Is there a sudo for os.remove() - python

I have an existing file I want to remove, and I get the following error when trying to remove it:
os.remove(input_path)
OSError: [Errno 13] Permission denied:
Is there any way to remove a file that already exists, other than doing:
subprocess.call(['rm', input_path])

Since you're getting a "permission denied" error, it is clear there's a "mismatch" between the permissions of the file (or its parent directory), and those of the user running the python process.
The best practice, instead of looking for "shortcuts" in the form of sudo, is to fix the permissions, either of the file being deleted, or the user running the python process.
Permissions are used for a reason. You're risking getting into troubles if you choose to void/bypass them by using tricks such as sudo.

Related

PermissionError: [Errno 1] for os.rename as quick action

This question represents the latest issue encountered when solving another problem. Let me explain the current issue first and leave the initial difficult at the end.
os.rename(old_path, new_path)
This Python command works when running the Python script in Automator (using the the run button on the top right).
/usr/local/bin/python3 "/Users/user/Library/Scripts/move.py"
However, when run as a quick action, it throws the error
The action “Run Shell Script” encountered an error: “Traceback (most recent call last):
File "/Users/user/Library/Scripts/move.py", line 6, in <module>
os.rename(folder_path, folder_nath)
PermissionError: [Errno 1] Operation not permitted:
All suggested solution tell me to grant various programmes full disk access. However, after having done so for Terminal, Finder, Automator and bash, I still see the error. I even changed the permissions for the folder to be renamed in the 'Get info' box. Nothing has helped.
This problems seems to be isomorphic with a problem I had earlier with another commend not working as a quick action:
os.system("""/bin/mv \"""" + old_path + """\" \"""" + new_path + """\"""")
This command works in PyCharm; it works when I run the python code containing it in Automator (using the Run button in the top right).
/usr/local/bin/python3 "/Users/user/Library/Scripts/move.py"
However, it does not work when I run this workflow as a quick action triggered by a keyboard short-cut: the quick action clearly works as the little cog rotates, but the file remains unchanged.
I have solved this issue. When it comes to quick actions like this, you need to grant full disk-access permission to the application open when you use the quick action.
As I use this quick action when using, for example, TextEdit, I needed to give TextEdit full disk-access permission.

Cannot get Perforce Triggers to work properly

Been trying to get this working all day, and i just can't figure out why its not working.
Trying to implement a simple trigger to run when the user submitted a file.
example in the .tmp file:
hello_trigger change-submit //testDepot/... "python F:/triggers/hello_trigger.py"
when i try to submit a file i get this:
Submit validation failed -- fix problems then use 'p4 submit -c 10199'.
'hello_trigger' validation failed: python: can't open file 'F:/triggers/hello_trigger.py': [Errno 2] No such file or directory
File exists and can be read, so its not a python issue.. same error with a .txt or .bat file.
From what i can gather the problem seems to be coming from the depot line in the trigger.
//testDepot/... fails
//depot/... doesnt fail, but the script is never fired off.
Any suggestions are greatly appreciated.
also testDepot is a stream not sure if that matters.
python: can't open file 'F:/triggers/hello_trigger.py': [Errno 2] No such file or directory
seems pretty clear that the file doesn't exist, at least from the point of view of this trigger command. Some things to double check:
This is running on the server machine, i.e. the place where the p4d service is running. If you have the script on your client machine the Python executable on the server isn't going to be able to find it!
Similarly, this is being run by whatever user is running p4d (on Windows this is usually the SYSTEM user, which may have limited permissions). Does that user have permission to read this path?
Could it be that your version of Python on Windows doesn't know how to handle paths with Unix-style forward slashes? (Many tools will normalize these for you but you shouldn't depend on it!) Try using a valid Windows path, i.e. F:\triggers\hello_trigger.py.

Windows permission error to delete joblibmem mapping folder in python

I am trying to run a python code that performs XGboosting and I wanted it to run parallely to take less time in building a model. But I am facing this issue while running a code.
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\<<username>>\\AppData\\Local\\Temp\\joblib_memmapping_folder_85680_9566857635\\85680-1746225537432-968de5958f0642829c37f0f09f0e8b00.pkl'
I have even tried running the anaconda prompt as an administrator. But it is of no use. As a workaround I have also tried what is suggested in https://github.com/joblib/joblib/issues/806 but even then I am facing the same issue.
Could you please advice?

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.)

PermissionError while running python program on windows

I've been tinkering with the os module for a few days encountered this error. Can't seem to fix it.
Here is an example:
import os
os.chdir('C:\\Users\\User\\Desktop')
os.rename('odin', 'ddin')
print (os.listdir())
And this is the error:
PermissionError: [WinError 5] Access is denied: 'odin' -> 'ddin'
Any help?
you are running your python program from an user that does not have permissions to change the specific file name ' try running from another user or change the file permissions to allow writing to your user.
First, try running the same program in IDLE with administrator privileges.
Second, There is chance that your anti-virus software is blocking your python script so try disabling antivirus.

Categories

Resources