Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20979

Re: check if directory is writable in a portable way

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!ecngs!feeder.ecngs.de!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <mail@timgolden.me.uk>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'permissions': 0.04; 'think,': 0.05; 'ugly': 0.07; 'from:addr:timgolden.me.uk': 0.09; 'from:name:tim golden': 0.09; 'message-id:@timgolden.me.uk': 0.09; 'url:github': 0.09; 'api': 0.09; 'am,': 0.12; 'library': 0.13; 'subsequent': 0.13; '-tkc': 0.16; 'itch,': 0.16; 'oserror:': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'things?': 0.16; 'cc:addr :python-list': 0.16; 'looked': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'possibly': 0.19; "haven't": 0.20; 'maybe': 0.21; 'header:In- Reply-To:1': 0.22; '(or': 0.22; 'fine': 0.24; 'right.': 0.25; 'tests': 0.25; 'guess': 0.26; 'code,': 0.28; 'depends': 0.28; "i'm": 0.28; 'universal': 0.29; 'cc:addr:python.org': 0.29; 'unable': 0.30; 'tjg': 0.30; "didn't": 0.30; "i've": 0.32; 'does': 0.32; "isn't": 0.33; 'there': 0.33; 'header:User-Agent:1': 0.33; 'file': 0.34; 'try:': 0.34; 'something': 0.35; 'cc:2**1': 0.36; 'but': 0.37; 'either': 0.37; 'enough': 0.38; 'received:192': 0.38; 'think': 0.38; 'should': 0.38; 'skip:o 20': 0.38; 'files': 0.39; 'fail': 0.39; 'right,': 0.39; 'except': 0.39; 'tasks': 0.39; 'might': 0.40; '(1)': 0.40; 'more': 0.61; 'kindly': 0.62; 'afraid': 0.63; 'results': 0.64; 'from:addr:mail': 0.64; 'ever': 0.64; 'encountered': 0.73; '11:34': 0.84; 'andrea': 0.84; 'ugly,': 0.84; 'abstracts': 0.91
Date Tue, 28 Feb 2012 13:33:52 +0000
From Tim Golden <mail@timgolden.me.uk>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2
MIME-Version 1.0
To Andrea Crotti <andrea.crotti.0@gmail.com>
Subject Re: check if directory is writable in a portable way
References <4F4CA76B.5040408@gmail.com> <4F4CBBE3.5060108@tim.thechases.com> <4F4CC1FC.40502@gmail.com>
In-Reply-To <4F4CC1FC.40502@gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.227.1330436089.3037.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330436089 news.xs4all.nl 6889 [2001:888:2000:d::a6]:39683
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20979

Show key headers only | View raw


On 28/02/2012 12:01, Andrea Crotti wrote:
> On 02/28/2012 11:34 AM, Tim Chase wrote:
>> On 02/28/12 04:07, Andrea Crotti wrote:
>>> How should I check if I can create files in a directory?
>>>
>>> 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
>>
>> It depends on the system & location. It's possible to set up
>> directories with permissions that allow you to create files but not
>> delete them, in which case you'd either (1) create the file and
>> possibly fail on subsequent tests because the file already exists; or
>> (2) litter the directory with tmpnam()-like results that you were
>> unable to delete.
>>
>> It's ugly, I've encountered it, and haven't found a good universal
>> solution myself.
>>
>> -tkc
>>
>
> That's really ugly right, didn't think about this possibility.
> Well it's not a really critical part of my code, so I guess it's fine
> with the try-except dance..
>
> But isn't there (or should there be) a windows-related library that
> abstracts this horrible things?

Well I maintain winsys [1] which does help out in admin-y tasks
in general on Windows, but I'm afraid I've never had the need
to scratch this particular itch, and the API solution is messier
than you might think, and more fiddly to get right. If I ever
get enough moments together I might do it, but patches are
always looked upon kindly :)

TJG

[1] https://github.com/tjguk/winsys

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: check if directory is writable in a portable way Tim Golden <mail@timgolden.me.uk> - 2012-02-28 13:33 +0000

csiph-web