Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > for-loop on cmd-line

Reply
Thread Tools

for-loop on cmd-line

 
 
Gisle Vanem
Guest
Posts: n/a
 
      10-11-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
 
Reply With Quote
 
 
 
 
suzaku
Guest
Posts: n/a
 
      10-11-2012
According to the document (http://docs.python.org/using/cmdline...erface-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

 
Reply With Quote
 
 
 
 
suzaku
Guest
Posts: n/a
 
      10-11-2012
According to the document (http://docs.python.org/using/cmdline...erface-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57