python -c commands on windows.

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Oct 22 06:42:41 EDT 2013


On 21 October 2013 21:47, Terry Reedy <tjreedy at udel.edu> wrote:
> Manual says "-c <command>
>     Execute the Python code in command. command can be one or more
> statements separated by newlines, with significant leading whitespace as in
> normal module code."
>
> In Windows Command Prompt I get:
> C:\Programs\Python33>python -c "a=1\nprint(a)"
>   File "<string>", line 1
>     a=1\nprint(a)
>                 ^
> SyntaxError: unexpected character after line continuation character
> (Same if I remove quotes.)
>
> How do I get this to work?

By not using cmd.exe.

On Windows I use "git bash" which I think is just bash from msys. If
you find yourself spending any time using cmd.exe you'll appreciate
why. I also use console2 as the GUI part of the terminal.

In bash it's a simple matter of opening a single quote and then typing
what you want including hitting enter (don't use single quotes in the
Python code):

$ python -c '
> a = 1
> if a:
>   print("a was true")
> '
a was true


Oscar



More information about the Python-list mailing list