Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'python.': 0.04; 'raised': 0.07; "(i'm": 0.09; 'alter': 0.09; 'etc...': 0.09; 'occurrences': 0.09; 'files.': 0.09; 'bash,': 0.16; 'cc:name:python list': 0.16; 'dirs,': 0.16; 'exists?': 0.16; 'hierarchy': 0.16; 'no-op': 0.16; 'oserror': 0.16; 'slave': 0.16; 'unwound': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'meant': 0.17; 'wrote:': 0.18; 'exists.': 0.18; 'instance': 0.18; 'subject:skip:s 10': 0.18; '(which': 0.19; 'cheers,': 0.20; 'subject:help': 0.21; 'trying': 0.21; "doesn't": 0.22; 'header:In- Reply-To:1': 0.22; 'assume': 0.22; 'feb': 0.22; 'clause': 0.23; 'ignore': 0.24; 'stack': 0.24; 'received:209.85.220': 0.25; 'mode': 0.25; 'cc:2**0': 0.26; 'stuff': 0.26; 'function': 0.27; 'there.': 0.27; 'tried': 0.27; "i'm": 0.28; 'pass': 0.29; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'parent': 0.30; 'reached.': 0.30; 'sun,': 0.30; 'chris': 0.30; '(e.g.': 0.31; 'does': 0.32; 'there': 0.33; 'instead': 0.33; 'file': 0.34; 'steven': 0.34; 'done': 0.34; 'identical': 0.34; 'copying': 0.35; 'something': 0.35; 'two': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'subject:with': 0.37; 'not,': 0.38; 'received:209.85': 0.38; 'except': 0.39; 'received:209': 0.39; 'might': 0.40; 'your': 0.61; 'collect': 0.61; 'collection': 0.69; 'yourself': 0.69; '12:14': 0.84; 'abandon': 0.84; 'sender:addr:chris': 0.84; 'slaves': 0.84; 'url:rebertia': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=9DiG6nGDABZNHmpJvpFwCUumv8TzfoxKxZkk03bmgtM=; b=N3gu5UyZ556P7v1p3wM9XPWeTSJqs6X9DF1AT8H02wNW/af0CQkRdaje5x++pEZ/5K evZmnCk68tKoeiAIs7S3UzGG8Crbzk12/OYwGlOtPpX3bjzCAlGypwVCFrC8df9ojFHQ oaPnWhumLeAGC0ndoazCgfEcpVA6Xn5UfAHWQ= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <4F381DA0.4020505@syslang.net> References: <4F381DA0.4020505@syslang.net> Date: Sun, 12 Feb 2012 13:09:51 -0800 X-Google-Sender-Auth: tHm3JV53QZHM-oSvV3M-caQyVVc Subject: Re: Need help with shutils.copytree From: Chris Rebert To: "Steven W. Orr" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQl8oLuHr+VuqlASB0cBRBvaJWlev74YGoHcqadCZZNvr+mJvD2OdzcbE1xyGh+1NlZklreX Cc: python list 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: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1329080995 news.xs4all.nl 6852 [2001:888:2000:d::a6]:54860 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20294 On Sun, Feb 12, 2012 at 12:14 PM, Steven W. Orr wrote: > I have a 'master' directory and a collection of 'slave' dirs. I want the > master to collect all of the stuff in the slave dirs. > > The slaves all look like this, > > . > |-- slaveX > | =C2=A0 `-- archI > | =C2=A0 | =C2=A0 `-- distJ > | =C2=A0 | =C2=A0 | =C2=A0 ` -- FILE > > Where the different slaveX dirs may contain multiple occurrences of archI > and distJ, but across all slaveX dirs, there will only be one *unique* > instance of FILE in archI and distJ. > > Here's an example: Given slave[1234], arch1 and arch2, and dist1 and dist= 2, > I want master to end up looking like this: > > . > |-- master > | =C2=A0 `-- arch1 > | =C2=A0 | =C2=A0 ` -- dist1 > | =C2=A0 | =C2=A0 | =C2=A0 =C2=A0` -- FILE > | =C2=A0 `-- arch1 > | =C2=A0 | =C2=A0 ` -- dist2 > | =C2=A0 | =C2=A0 | =C2=A0 =C2=A0` -- FILE > | =C2=A0 `-- arch2 > | =C2=A0 | =C2=A0 ` -- dist1 > | =C2=A0 | =C2=A0 | =C2=A0 =C2=A0` -- FILE > | =C2=A0 `-- arch2 > | =C2=A0 | =C2=A0 ` -- dist2 > | =C2=A0 | =C2=A0 | =C2=A0 =C2=A0` -- FILE > > etc... You have multiple directories at the same level in the hierarchy with identical names (e.g. two "arch1"s), which is invalid. I assume you meant for them to be combined? > In bash, I might use cpio passthrough mode and say something like: > > master=3D$path_to_master > for slave in ${slaves} > do > =C2=A0 =C2=A0pushd $slave > =C2=A0 =C2=A0find . -print | cpio -pdum $master > =C2=A0 =C2=A0popd > done > > but I'm having a hard time trying to get this functionality in python. (I= 'm > trying to avoid writing a subprocess.) > > I tried using shutil.copytree with a try / except that does a pass on > OSError (which is what gets raised when trying to create a dir that alrea= dy > exists). No joy there. Right; the stack has already been unwound by the time your `except` clause is reached. You just need to recover by instead copying the children of the subtree individually yourself when their parent already exists. For example: master/arch1 already exists? Then copy the slave/arch1/distN-s individually. Or alternately, abandon copytree() entirely: you just LYBL and check if the parent directories already exist; if not, you try to create the directories yourself; and finally, you copy the individual files. [Useful funcs: os.listdir(), os.path.exists(), os.mkdir() / os.mkdirs()] > I also tried an ignore function that always returns (). That's an effective no-op which doesn't alter copytree()'s behavior whatsoe= ver. Cheers, Chris -- http://rebertia.com