Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72275

Re: Multi-line commands with 'python -c'

References <CAPTjJmoGTu4s0+rwfGVjnfeFfqxOzQGevtL8vsxULBm=Ff+Ouw@mail.gmail.com> <CAKJDb-NxghzWch7mP58k8f_M6mq++yXdCoz+2vufejZR_F0Vug@mail.gmail.com>
Date 2014-05-30 12:47 +1000
Subject Re: Multi-line commands with 'python -c'
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.10473.1401418078.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, May 30, 2014 at 12:34 PM, Zachary Ware
<zachary.ware+pylist@gmail.com> 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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Multi-line commands with 'python -c' Chris Angelico <rosuav@gmail.com> - 2014-05-30 12:47 +1000

csiph-web