Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'problem:': 0.07; "'python": 0.09; 'arguments': 0.09; 'escape': 0.09; 'root,': 0.09; 'terminates': 0.09; 'translate': 0.10; 'python': 0.11; 'arg': 0.16; 'backslash': 0.16; 'does,': 0.16; 'files)': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'newlines': 0.16; "os'": 0.16; 'os;': 0.16; 'silence': 0.16; 'syntax,': 0.16; 'subject:python': 0.16; 'import': 0.22; 'shell': 0.22; 'to:name:python-list@python.org': 0.22; "aren't": 0.24; 'script.': 0.24; "haven't": 0.24; 'second': 0.26; 'pass': 0.26; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; 'critical': 0.32; 'option': 0.32; 'guess': 0.33; 'maybe': 0.34; 'subject:with': 0.35; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'that!': 0.36; 'virtual': 0.37; 'to:addr:python-list': 0.38; 'files': 0.38; 'list,': 0.38; 'little': 0.38; 'ability': 0.39; 'to:addr:python.org': 0.39; 'obvious': 0.74; "'for'": 0.84; 'subject:commands': 0.84; 'this...': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=4uZSQbPPtofLXbibQttMiIJFvxUlAHNjNJDdraen20w=; b=VsgXOembgoWhiG4iKZ3+u2yPMDc5dCWmeQ759kPrCCRzL/r9KOdnO7sw4VkFgvgEFf +YWh4L3QLjk/sO2ujyTxtcQYZrk2v6p/WcLyCdnrqu6ZZarxA2Y7DjSxK0l2bQSJQNSr ckNF6jWgb4JCACOrOjEJsuwC79oAayzjqtS/E7C4f6Jx2KYrDA1mlhDKRS2nNg8uxqlS G4ssSe2KOt+mfUdUOmwI43BMpAn4rAZmrGnE9arR++rVD2l+NlmjA+qhJSeQZ8kIkEqg F/qsSDjQR5vzY0d9iAXqdKgzU+pRF0/lcrU6/TvEaM/nWXO99IJi4SzRQzSbiSOH89u5 kS7g== MIME-Version: 1.0 X-Received: by 10.58.136.168 with SMTP id qb8mr10220933veb.21.1401411144228; Thu, 29 May 2014 17:52:24 -0700 (PDT) Date: Fri, 30 May 2014 10:52:24 +1000 Subject: Multi-line commands with 'python -c' From: Chris Angelico To: "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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401411146 news.xs4all.nl 2860 [2001:888:2000:d::a6]:55160 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72267 Since lines are so critical to Python syntax, I'm a little surprised there's no majorly obvious solution to this... or maybe I'm just blind. Problem: Translate this into a shell one-liner: import os for root, dirs, files in os.walk("."): if len(dirs + files) == 1: print(root) Solution 1: SyntaxError python -c 'import os; for root, dirs, files in os.walk("."): if len(dirs + files) == 1: print(root)' You can't put a 'for' statement after an 'import' with just a semicolon. Solution 2: SyntaxError python -c 'import os\nfor root, dirs, files in os.walk("."): if len(dirs + files) == 1: print(root)' You can't put a backslash escape into your code like that! Makes no sense. Solution 3: Silence python -c 'import os' -c 'for root, dirs, files in os.walk("."): if len(dirs + files) == 1: print(root)' Haven't dug into exactly what this does, but the docs say that -c terminates the option list, so I would guess that the second -c and its arg get passed to the script. Solution 4: Rely on the shell's ability to pass newlines inside arguments $ python -c 'import os > for root, dirs, files in os.walk("."): > if len(dirs + files) == 1: print(root) > ' That works, but at that point, you aren't writing a one-liner any more. It's also fiddly to edit. Is there a better way to put multiple virtual lines into a 'python -c' command? ChrisA