Issue
I'm setting the mode on a file to try to prevent it being deletable, but nothing seems to work. Example:
import os
from stat import S_IRUSR, S_IRGRP, S_IROTH
with tempfile.TemporaryDirectory() as local_dir:
local_file = os.path.join(local_dir, 'a.txt')
with open(local_file, 'wt') as f:
f.writelines('some stuff')
os.chmod(local_file, S_IRUSR|S_IRGRP|S_IROTH)
print(oct(os.stat(local_file).st_mode)[-3:]) # prints '444' as expected
os.remove(local_file) # no exception
print(os.path.isfile(local_file)) # prints False, the file has been deleted
Solution
Running chmod on a file cannot render a file non-deletable. There is no "deletable/not deletable" mode bit.
A user's permissions for a file have no effect on whether they can delete that file. To delete a file, a user needs write and execute permissions on the containing directory.
Answered By - user2357112 Answer Checked By - Marie Seifert (WPSolving Admin)