Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20973 > unrolled thread
| Started by | Tim Golden <mail@timgolden.me.uk> |
|---|---|
| First post | 2012-02-28 10:50 +0000 |
| Last post | 2012-02-28 10:50 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: check if directory is writable in a portable way Tim Golden <mail@timgolden.me.uk> - 2012-02-28 10:50 +0000
| From | Tim Golden <mail@timgolden.me.uk> |
|---|---|
| Date | 2012-02-28 10:50 +0000 |
| Subject | Re: check if directory is writable in a portable way |
| Message-ID | <mailman.221.1330426255.3037.python-list@python.org> |
On 28/02/2012 10:07, Andrea Crotti wrote:
> 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?
This is remarkably complicated to do on Windows by
checking Security APIs etc. If the try:except dance
works for you, I recommend that you use it. (In the
past, people who have asked this question have not
wanted to use try-except because they didn't want the
overhead of creating even a zero-length file)
TJG
Back to top | Article view | comp.lang.python
csiph-web