delete files/dirs with read-only mode

Ben Hutchings do-not-spam-ben.hutchings at businesswebsoftware.com
Fri May 2 09:56:24 EDT 2003


In article <mailman.1051819623.3155.python-list at python.org>,
Fredrik Lundh wrote:
> Steven Taschuk wrote:
> 
>> And on Unixy systems, chmod.  I don't know offhand of a
>> platform-independent way of removing the read-only attribute,
>> sadly.
> 
> os.chmod(filename, 0666) tends to work on most platforms.

There isn't a problem under Unix because write permission is not the
same thing as delete permission (which requires write permission on
the directory, and ownership of the file if the directory has the
sticky bit set).  However, your suggested solution *is* a problem
because it makes the files world-readable before they are unlinked
(note - not necessarily deleted).

When I've had to implement this I've used an except block that checks
whether the errno is EACCES and the OS is Windows, and in that case
does an os.chmod(filename, 0600) and retries.  An alternative would
be to check the OS and do the chmod in advance on Windows.




More information about the Python-list mailing list