Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed20.multikabel.net!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '"""': 0.07; 'apis': 0.07; 'false,': 0.09; 'from:addr:timgolden.me.uk': 0.09; 'from:name:tim golden': 0.09; 'function:': 0.09; 'message- id:@timgolden.me.uk': 0.09; 'writable': 0.09; 'def': 0.13; '"""return': 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; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'file)': 0.18; 'maybe': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'asked': 0.24; 'dance': 0.24; 'creating': 0.25; 'windows': 0.26; 'cc:2**0': 0.26; '(in': 0.26; 'tried': 0.27; 'example': 0.29; 'cc:addr:python.org': 0.29; 'tjg': 0.30; "didn't": 0.30; 'header:User-Agent:1': 0.33; 'it.': 0.33; 'file': 0.34; 'checking': 0.34; 'try:': 0.34; 'something': 0.35; 'question': 0.36; 'but': 0.37; 'received:192': 0.38; 'should': 0.38; 'skip:o 20': 0.38; 'files': 0.39; 'except': 0.39; 'user': 0.40; 'from:addr:mail': 0.64; 'andrea': 0.84; 'remarkably': 0.84; 'to:none': 0.93 Date: Tue, 28 Feb 2012 10:50:51 +0000 From: Tim Golden User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 MIME-Version: 1.0 CC: python-list Subject: Re: check if directory is writable in a portable way References: <4F4CA76B.5040408@gmail.com> In-Reply-To: <4F4CA76B.5040408@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1330426255 news.xs4all.nl 6854 [2001:888:2000:d::a6]:52540 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20973 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