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: 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; 'counting': 0.09; 'if,': 0.09; 'restriction': 0.09; 'script,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '"import': 0.16; 'cleaner': 0.16; 'file):': 0.16; 'for,': 0.16; 'looping': 0.16; 'semicolon': 0.16; 'statements,': 0.16; 'syntaxerror:': 0.16; 'sys.path:': 0.16; 'wrote:': 0.17; 'restrictions': 0.17; '(in': 0.18; 'variable': 0.20; '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; 'am,': 0.27; 'run': 0.28; "i'm": 0.29; 'file': 0.32; 'print': 0.32; 'defining': 0.33; "can't": 0.34; 'program,': 0.34; 'list': 0.35; 'there': 0.35; 'list.': 0.35; 'skip:p 20': 0.36; 'enough': 0.36; 'turn': 0.36; 'being': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'several': 0.39; 'instead': 0.39; 'received:192': 0.39; 'easily': 0.39; 'received:192.168': 0.40; 'subject:-': 0.40; 'your': 0.60; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'received:74.208.4.194': 0.84 Date: Thu, 11 Oct 2012 07:54:33 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Gisle Vanem Subject: Re: for-loop on cmd-line References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:zgSbFIfIOYXZEyTEs3HJA9za+nigL0Hqc1OQvtWov5e EVNjXJ3JSl5XfB+lfVBy64lpV33OH/PawvUCMDKa7ReizUdt/q q304YVdTrAiniYViEqXUsz20alNIhiEuXA5F7gp/HZSE2Fngdb uXx6fvu2/kOgrYHFPiTyBvUMx2JZ/qvPcb3E4BZwxoLyqlg4PJ 3Z2y8HT1TDTQzHE28q7ejuA2ejAvmFD3HTfGbiCO/yAuqKElAT oCmXSWDB2LCgdS9TYVIIjEzWI9b4Nxa8PX2No6P3OxLS6m0Crj IyhZrX/Z0t2Bi6JLq4IjQCOR5Gs9W3Z/qMC/DpktSfpnydOuQ= = Cc: Python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349956499 news.xs4all.nl 6980 [2001:888:2000:d::a6]:42159 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31111 On 10/11/2012 07:24 AM, 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 "", 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 it has nothing to do with being on a command line. You're using semicolon to combine several statements, and there are restrictions on what can be combined that way. One restriction is the looping constructs, for, if, while. Try experimenting with a standard program, to see what can be combined and what cannot. You can do it easily enough with a list comprehension. Let us know if you can't work that out. By the way, much cleaner than defining your own counting variable is to use enumerate(). Any reason why you don't just make a one-file python script, and run that instead of your one line batch file? Or is this line one of many in the batch file? -- DaveA