check if directory is writable in a portable way

Andrea Crotti andrea.crotti.0 at gmail.com
Tue Feb 28 05:07:39 EST 2012


How should I check if I can create files in a directory?

I tried to simply check if the directory is writeable with this function:

def is_writable(name):
     """Return true if the file is writable from the current user
     """
     return os.access(name, os.W_OK)

but that doesn't work at all on Windows, because for example if I create 
a new directory
and do os.access(dirpath, os.W_OK) it returns false, even if I can 
create files inside without problems.

So maybe the only solution that works is something like
try:
     open(path.join('temp', 'w'))
except OsError:
     return False
else:
     os.remove(path.join('temp'))
     return True

would it make sense?



More information about the Python-list mailing list