for-loop on cmd-line

Discussion in 'Python' started by Gisle Vanem, Oct 11, 2012.

  1. Gisle Vanem

    Gisle Vanem Guest

    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
    Gisle Vanem, Oct 11, 2012
    #1
    1. Advertising

  2. Gisle Vanem

    suzaku Guest

    According to the document (http://docs.python.org/using/cmdline.html#interface-options),
    > When called with -c command, it executes the Python statement(s) given as command. Here command may contain multiple statements separated by newlines. Leading whitespace is significant in Python statements!


    So you should replace the semicolon with newline.

    BTW, the loop can be simplified using `enumerate` like this:

    for i, p in enumerate(sys.path):


    On Thursday, October 11, 2012 7:24:31 PM UTC+8, Gisle Vanem wrote:
    > 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
    suzaku, Oct 11, 2012
    #2
    1. Advertising

  3. Gisle Vanem

    suzaku Guest

    According to the document (http://docs.python.org/using/cmdline.html#interface-options),
    > When called with -c command, it executes the Python statement(s) given as command. Here command may contain multiple statements separated by newlines. Leading whitespace is significant in Python statements!


    So you should replace the semicolon with newline.

    BTW, the loop can be simplified using `enumerate` like this:

    for i, p in enumerate(sys.path):


    On Thursday, October 11, 2012 7:24:31 PM UTC+8, Gisle Vanem wrote:
    > 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
    suzaku, Oct 11, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Achim Domma (Procoders)

    read input for cmd.Cmd from file

    Achim Domma (Procoders), Jun 3, 2005, in forum: Python
    Replies:
    2
    Views:
    8,056
    Peter Otten
    Jun 3, 2005
  2. Sarir Khamsi

    Interpreter-like help in cmd.Cmd

    Sarir Khamsi, Jun 9, 2005, in forum: Python
    Replies:
    4
    Views:
    362
    Bengt Richter
    Jun 26, 2005
  3. =?ISO-8859-1?Q?Sch=FCle_Daniel?=

    [exec cmd for cmd in cmds]

    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Mar 8, 2006, in forum: Python
    Replies:
    3
    Views:
    382
    Scott David Daniels
    Mar 8, 2006
  4. Diez B. Roggisch

    pydb remote debugging/cmd.Cmd over socket?

    Diez B. Roggisch, May 28, 2008, in forum: Python
    Replies:
    2
    Views:
    544
    Diez B. Roggisch
    May 29, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page