python -c commands on windows.
Terry Reedy
tjreedy at udel.edu
Mon Oct 21 18:39:06 EDT 2013
On 10/21/2013 5:14 PM, random832 at fastmail.us wrote:
> On Mon, Oct 21, 2013, at 16:47, Terry Reedy 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?
>
> Well, ignoring the "why would you want to" factor...
Because it is claimed to be possible ;-)
That particular example could be written in one line with ';'. But it is
a minimal example that either shows the problem or obviously works. (IE,
what we recommend people post ;-). I initially tried to test a version
of Chris Angelico's code from another thread without writing it to a file:
def a():
def b():
nonlocal c
I also wanted to know if 'newline' in the doc meant a literal newline
(it appears so, but it seemed impossible on Windows) rather than '\n'
and if the problem was peculiar to Windows. And I need to know that to
run "python -c ... " in a subproccess from Python.
> this actually _is_ possible.
Even on Windows .. I presume this is easier on *nix.
> C:\>python -c a=1^
> More?
> More? print(a)
> 1
>
> You can put quotes around any part of the command you need spaces in,
> but you _cannot_ have the ^ in quotes. So, with quotes, it would be as
> follows:
>
> C:\>python -c "a='1 2'"^
> More?
> More? print(a)
> 1 2
Spaces would be typical for a compound statememt header line. For a
third line, the second should have ^ added.
> This is a very obscure feature of the command processor,
Really obscure ;-)
> and I don't know if it works inside a batch file.
It does:
---tem.bat
python -c "def f(a):"^
" print(a+1)"^
f(2)
---Command Prompt
C:\Programs\Python33>\Users\Terry\Documents\tem
C:\Programs\Python33>python -c "def f(a):"
" print(a+1)"
f(2)
3
---
3 lines echoed (they could have been suppressed) plus output.
--
Terry Jan Reedy
More information about the Python-list
mailing list