check if directory is writable in a portable way
Mark Hammond
skippy.hammond at gmail.com
Tue Feb 28 18:32:10 EST 2012
On 28/02/2012 9:07 PM, Andrea Crotti wrote:
> How should I check if I can create files in a directory?
By trying to create them there :) Presumably you want to know that so
you can write something "real" - so just write that something real.
The problem gets quite hard when you consider things like elevation -
your *user* may have rights to write to a directory but only when
elevated - think writing into Program Files. Further, this check can
only ever be transient - what if you have the rights by virtue of a
group membership, but tomorrow you are no longer in that group? Or by
virtue of being the "owner" of the directory but later losing the ownership?
The only reasonable way to check is to write to it, and you may as well
skip attempting to write a temp file - just write what you care about
and handle failure in the most graceful way you can. This is what
almost every app does - consider apps with a "save as" dialog - they
never check the directory is writable, they just attempt the actual
write and handle the failure.
Mark
More information about the Python-list
mailing list