When will os.remove fail?

Steve D'Aprano steve+python at pearwood.info
Sun Mar 12 13:48:58 EDT 2017


On Linux, if I call os.remove on a file which I own but don't have write
permission on, the file is still deleted:


py> f = open('/tmp/no-write', 'w')
py> os.path.exists('/tmp/no-write')
True
py> os.chmod('/tmp/no-write', 0)  # Forbid ALL access.
py> os.remove('/tmp/no-write')
py> os.path.exists('/tmp/no-write')
False


It seems that os.remove on Linux will force the delete even if the file is
read-only or unreadable, provided you own the file.

Does os.remove work like this under Windows too?

Under what circumstances will os.remove fail to remove a file?

If you don't own the file and have no write permission, if it is on
read-only media, anything else?


Thanks,



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.



More information about the Python-list mailing list