Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.roellig-ltd.de!open-news-network.org!feed.xsnews.nl!fbe001.ams.xsnews.nl!ecngs!feeder.ecngs.de!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.037 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'counting': 0.09; 'root,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'wrote': 0.14; 'files)': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subdirectory': 0.16; 'task:': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; "python's": 0.19; '(the': 0.22; 'import': 0.22; 'shell': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'on,': 0.29; "doesn't": 0.30; 'involving': 0.30; 'message- id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; 'pipe': 0.31; 'really,': 0.31; 'though.': 0.31; 'file': 0.32; 'fri,': 0.33; 'could': 0.34; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'much.': 0.36; "didn't": 0.36; 'being': 0.38; 'files': 0.38; 'pm,': 0.38; 'rather': 0.38; 'commands': 0.60; 'simple': 0.61; '30,': 0.65; 'competition': 0.65; 'statement,': 0.68; 'theoretical': 0.74; 'heh.': 0.84; 'subject:commands': 0.84; 'ware': 0.91; 'to:none': 0.92 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:cc :content-type; bh=S0g4LxjJF9X4ZL+N6zjJKALqEUK6a/T6CjoNTk5dfss=; b=FfCgF2K3iCbw86sVXNWjSqFqxgUIP7An8oQ39wPo8n8FlHsdwIXMOoLaOnN77JYUL1 du4RfPo9uU7FEr2siWyXv/O4Ffpx55gCJSBvMkVtYm80X71MarhBBxD4VgvCpfNM1bcq PwJWX08rudm9jxDIPpRMF/M0mIB7VnBaiK4oZWYyegmQwiTo2C/KO/JJRsxIYPIeafPH mpYLEK84lCPlrvUk446EyzF/YjbG1TloO0XctuH7B3Vc9ix4ZpU2L2E7V22pcv4oNi6X wUas2Wt785tyjkJOE27AiFfVAar3Y0/1Y1nQWRyS12e2IzIu+a5DE9Kg1+xilqGWGItK op5w== MIME-Version: 1.0 X-Received: by 10.221.9.72 with SMTP id ov8mr5113334vcb.27.1401418074903; Thu, 29 May 2014 19:47:54 -0700 (PDT) In-Reply-To: References: Date: Fri, 30 May 2014 12:47:54 +1000 Subject: Re: Multi-line commands with 'python -c' From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401418078 news.xs4all.nl 2867 [2001:888:2000:d::a6]:50156 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72275 On Fri, May 30, 2014 at 12:34 PM, Zachary Ware wrote: > You can always cheat: > > $ python -c 'exec("import os\nfor root, dirs, files in os.walk(\".\"):\n if > len(dirs + files) == 1: print(root)")' > > Doesn't do much for being long and fiddly, though. Not really, no! Heh. I wrote that in competition against a theoretical solution involving shell commands and pipes and so on, with the intention being that the Python version could be a simple shell one-liner, just as the pipe version could. (The task: Find all directories with exactly one subdirectory or file in them, not counting dot and dot-dot. Python's os.walk() is pretty much perfect for that.) I tried fiddling with __import__() rather than the import statement, but it didn't really aid clarity much. ChrisA