Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31108 > unrolled thread
| Started by | Gisle Vanem <gvanem@broadpark.no> |
|---|---|
| First post | 2012-10-11 13:24 +0200 |
| Last post | 2012-10-11 04:50 -0700 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
for-loop on cmd-line Gisle Vanem <gvanem@broadpark.no> - 2012-10-11 13:24 +0200
Re: for-loop on cmd-line suzaku <satorulogic@gmail.com> - 2012-10-11 04:50 -0700
Re: for-loop on cmd-line suzaku <satorulogic@gmail.com> - 2012-10-11 04:50 -0700
| From | Gisle Vanem <gvanem@broadpark.no> |
|---|---|
| Date | 2012-10-11 13:24 +0200 |
| Subject | for-loop on cmd-line |
| Message-ID | <mailman.2043.1349954670.27098.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | suzaku <satorulogic@gmail.com> |
|---|---|
| Date | 2012-10-11 04:50 -0700 |
| Message-ID | <0656c8ec-07ef-4ff2-a43b-50caed6bb136@googlegroups.com> |
| In reply to | #31108 |
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
[toc] | [prev] | [next] | [standalone]
| From | suzaku <satorulogic@gmail.com> |
|---|---|
| Date | 2012-10-11 04:50 -0700 |
| Message-ID | <mailman.2044.1349956265.27098.python-list@python.org> |
| In reply to | #31108 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web