When will os.remove fail?

Larry Martell larry.martell at gmail.com
Sun Mar 12 14:04:09 EDT 2017


On Sun, Mar 12, 2017 at 1:48 PM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> 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.

In Linux, I believe you need write permission on the dir to delete a
file - the perms on the file do not matter.


More information about the Python-list mailing list