Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a sudo for os.remove()

Tags:

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])
like image 969
David542 Avatar asked Oct 18 '25 23:10

David542


1 Answers

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.

like image 96
shx2 Avatar answered Oct 21 '25 13:10

shx2