for-loop on cmd-line

Gisle Vanem gvanem at broadpark.no
Thu Oct 11 07:24:22 EDT 2012


Hello list. I'm a newbie when it comes to Python.

I'm trying to turn this:

 def print_sys_path():
    i = 0
    for p in sys.path:
      print ('sys.path[%2d]: %s' % (i, p))
      i += 1

into a one-line python command (in a .bat file):

  python -c "import sys,os; i=0; for p in sys.path: print('sys.path[%%2d]: %%s' %% (i, p)); i+=1"

But:
  File "<string>", line 1
    import sys,os; i=0; for p in sys.path: print('sys.path[%2d]: %s' % (i, p)); i+=1
                          ^
SyntaxError: invalid syntax

The caret is on the 'for'. What's the problem?

--gv



More information about the Python-list mailing list