Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'finished,': 0.07; 'subject:skip:s 10': 0.07; 'test,': 0.07; 'executed': 0.09; 'suppress': 0.09; 'def': 0.12; 'exists,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function;': 0.16; 'instance:': 0.16; 'path:': 0.16; 'subject:issue': 0.16; 'subject:timing': 0.16; 'wrote:': 0.18; 'bit': 0.19; "skip:' 30": 0.19; 'error': 0.23; 'mon,': 0.24; "haven't": 0.24; 'permission': 0.26; 'possibly': 0.26; 'skip:" 30': 0.26; 'skip:" 40': 0.26; 'pass': 0.26; 'header:In-Reply- To:1': 0.27; 'tim': 0.29; 'errors': 0.30; 'message- id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '(since': 0.31; 'directory,': 0.31; 'exceptions': 0.31; 'raised': 0.31; 'file': 0.32; 'there.': 0.32; "i'd": 0.34; 'problem': 0.35; 'created': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'remote': 0.38; 'skip:o 20': 0.38; 'handle': 0.38; 'whatever': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'remove': 0.60; 'removing': 0.60; 'then,': 0.60; 'new': 0.61; 'first': 0.61; 'name': 0.63; 'worth': 0.66; 'jul': 0.74; 'removal': 0.74; 'mount': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=KfR3FrqaQDRPfNSJBiJY4FrUDh486ajpgj8r3Eo6JbA=; b=duFxINIU3WB9rVkxheSsYlEOX6nd9mA9lvUZZDkmFqzMgSV0Pn0eq/FTy9gKmpELxp 0uhbxfE7Ssx01Tc5Mc4fKJVkCWWn8PhqwmwKvQg31kQx+dNBCmMW92+dG9Z23J/2aOXL wsTJGkmxLWjIUFNecueAb2aQLuLdm1rsBqSnlI5N3QdXKZNHJumpJRzOVwgI9DGvb9yi 1Va3nZaQ2X9WIW410rNLWoK7kyEoRT3DcCEojNAucZLOMGpoMTlYqzthUBVn0OUNHD+F KBoVz38tdlCt5cGMje6AP7PxmRlB43sZOVJ/b7gmySHEZS7zRULcW7qQD2P/HGLqhn8d M+KA== MIME-Version: 1.0 X-Received: by 10.52.178.194 with SMTP id da2mr11525994vdc.28.1375141956594; Mon, 29 Jul 2013 16:52:36 -0700 (PDT) In-Reply-To: References: Date: Tue, 30 Jul 2013 00:52:36 +0100 Subject: Re: timing issue: shutil.rmtree and os.makedirs From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375141959 news.xs4all.nl 15914 [2001:888:2000:d::a6]:52545 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51527 On Mon, Jul 29, 2013 at 8:16 PM, Tim wrote: > My intent is to pass it a directory name or path and if it exists, use shutil.rmtree to remove whatever is there (if it isn't a directory, try to unlink it); then use os.makedirs to create a new directory or path: > > def make_clean_dir(directory): > if os.path.exists(directory): > if os.path.isdir(directory): > shutil.rmtree(directory) > else: > os.unlink(directory) > os.makedirs(directory) > > The last bit of the traceback is: > File "/develop/myproject/helpers/__init__.py", line 35, in make_clean_dir > os.makedirs(directory) > File "/usr/local/lib/python2.7/os.py", line 157, in makedirs > mkdir(name, mode) > OSError: [Errno 17] File exists: '/users/tim/testing/testing_html' > > The directory 'testing_html' existed when I executed the function; First thing I'd check is: Did rmtree succeed? Try removing the makedirs and test it again; then, when your process has completely finished, see if the directory is there. If it is, the problem is in rmtree - for instance: * You might not have permission to remove everything * There might be a messed-up object in the file system * If the directory is a remote share mount point, the other end might have lied about the removal * Something might have been created inside the directory during the removal * Myriad other possibilities As I understand rmtree's docs, any errors *that it detects* will be raised as exceptions (since you haven't told it to suppress or handle them), but possibly there's an error that it isn't able to detect. Worth a test, anyhow. ChrisA