Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31110

Re: for-loop on cmd-line

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <satorulogic@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'syntax': 0.03; 'newbie': 0.05; 'problem?': 0.07; 'python': 0.09; "%s'": 0.09; 'command.': 0.09; 'executes': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '"import': 0.16; 'command,': 0.16; 'file):': 0.16; 'semicolon': 0.16; 'simplified': 0.16; 'syntaxerror:': 0.16; 'sys.path:': 0.16; 'wrote:': 0.17; '(in': 0.18; 'trying': 0.21; 'import': 0.21; '"",': 0.22; 'cc:2**0': 0.23; 'this:': 0.23; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'replace': 0.27; 'separated': 0.29; 'statements': 0.29; 'whitespace': 0.29; "i'm": 0.29; 'thursday,': 0.30; 'url:python': 0.32; 'file': 0.32; 'print': 0.32; '11,': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'list.': 0.35; 'url:org': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'turn': 0.36; 'october': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'url:docs': 0.38; 'called': 0.39; 'subject:-': 0.40; 'leading': 0.61; 'here': 0.65; 'received:209.85.216.184': 0.84
Newsgroups comp.lang.python
Date Thu, 11 Oct 2012 04:50:57 -0700 (PDT)
In-Reply-To <mailman.2043.1349954670.27098.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=106.187.46.19; posting-account=moCW3wkAAACF8gjZjOJdCNj52AhCcg4V
References <mailman.2043.1349954670.27098.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 106.187.46.19
MIME-Version 1.0
Subject Re: for-loop on cmd-line
From suzaku <satorulogic@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Cc Python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.2044.1349956265.27098.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1349956265 news.xs4all.nl 6981 [2001:888:2000:d::a6]:39562
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31110

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web