Python interpreter invocation with "-c" and indentation issues

Posted by alexander on Stack Overflow See other posts from Stack Overflow or by alexander
Published on 2010-12-29T14:39:43Z Indexed on 2010/12/29 14:53 UTC
Read the original article Hit count: 235

I'm trying to invoke Python using the "-c" argument to allow me to run some arbitrary python code easily, like this: python.exe -c "for idx in range(10): print idx" Now this code works fine, from within my batch file, however, I'm running into problems when I want to do anything more than this.

Consider the following Python code:

foo = 'bar'
for idx in range(10):
    print idx

this would then give you 0-9 on the stdout. However, if I collapse this into a single line, using semicolons as delimiters, to get the following:

foo = 'bar';for idx in range(10):    print idx

and try to run it using python.exe -c it get a SyntaxError raised:

C:\Python>python.exe -c "foo = 'bar';for idx in range(10):    print idx"
  File "<string>", line 1
    foo = 'bar';for idx in range(10):    print idx
                  ^
SyntaxError: invalid syntax

Anyone know how I can actually use this without switching to a separate .py file?

© Stack Overflow or respective owner

Related posts about python

Related posts about interpreter