Multi-line commands with 'python -c'

Zachary Ware zachary.ware+pylist at gmail.com
Thu May 29 22:34:59 EDT 2014


On Thursday, May 29, 2014, Chris Angelico <rosuav at gmail.com
<javascript:_e(%7B%7D,'cvml','rosuav at gmail.com');>> wrote:

> 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)

<snip>

>
> Is there a better way to put multiple virtual lines into a 'python -c'
> command?
>

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.

--
Zach

Sent from an iPad, please bear with any brevity, uncaught
auto-uncorrections, HTML, or other annoyances. Suggestions for a great (not
just 'good') email app are welcome.


-- 
Sent from Gmail Mobile
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140529/c42f83bb/attachment.html>


More information about the Python-list mailing list