Multi-line commands with 'python -c'
Duncan Booth
duncan.booth at invalid.invalid
Sat May 31 12:41:21 EDT 2014
Chris Angelico <rosuav at gmail.com> wrote:
> On Sat, May 31, 2014 at 7:42 AM, Devin Jeanpierre
><jeanpierreda at gmail.com> wrote:
>> In unix shells you can literally use a new line. Or is that only
bash?
>
> You can in bash, I know, but it's fiddly to type it; and more
> importantly, it's not a good point in the "this is cleaner than a
> series of pipes" argument. My primary recommendation, of course, was a
> three-line script saved as an actual file, but for a more direct
> parallel to the pipe-it-three-ways model, I wanted to use -c.
and you also wrote originally that it's fiddly to edit. I think that
Windows Powershell has (at least in the current ISE command line) got
the editing a bit better. It's a minor difference though and it has
taken Microsoft about 30 years to get to that point.
What may be a larger difference, or may just be my lack of Linux-foo, is
this:
PS C:\python33> $script = @"
import os
for root, dirs, files in os.walk("."):
if len(dirs + files) == 1: print(root)
"@
PS C:\python33> python -c $script
.\Doc
.\Lib\concurrent\__pycache__
.\Lib\curses\__pycache__
...
which is a style I've found useful for example when running a group of
related timeit.py commands as I can put things like multi-line setup
statements in a variable and then have a simpler command to repeat.
But bash as far as I can won't let me do that:
$ script='import os
for root, dirs, files in os.walk("."):
if len(dirs + files) == 1: print(root)
'
$ python -c $script
File "<string>", line 1
import
^
SyntaxError: invalid syntax
--
Duncan Booth
More information about the Python-list
mailing list