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; 'newbie': 0.05; 'sys': 0.05; 'exit': 0.07; 'lines.': 0.07; 'skip:\\ 10': 0.07; 'scripts': 0.09; 'python': 0.09; "%s'": 0.09; "'''": 0.09; '@echo': 0.09; 'comment,': 0.09; 'def': 0.10; '(the': 0.15; 'weird': 0.15; '"exit': 0.16; 'echo': 0.16; 'exploits': 0.16; 'file):': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'oct': 0.16; 'rem': 0.16; 'sys.path:': 0.16; 'wrote:': 0.17; 'thu,': 0.17; '(in': 0.18; 'windows': 0.19; 'variable': 0.20; '+0200': 0.20; 'file.': 0.20; 'trying': 0.21; 'import': 0.21; 'this:': 0.23; 'command': 0.24; 'script': 0.24; 'header:In-Reply-To:1': 0.25; 'dos': 0.27; 'easiest': 0.27; 'message-id:@mail.gmail.com': 0.27; 'actual': 0.28; 'run': 0.28; "i'm": 0.29; 'code': 0.31; 'not.': 0.32; 'print': 0.32; '11,': 0.33; 'quotes': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'compared': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'list.': 0.35; 'but': 0.36; 'closing': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'turn': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'fact': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'most': 0.61; "you'll": 0.62; 'batch.': 0.84; '"one': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=aRb2GLVhl6afJSYcZO4gr7SfhEjSSyvu/VFh9xdriCI=; b=xef0ZRM8KToKhUR4F0O6qXGpN58HXnJMQ5X94swJtU/+4hgy4taI2AZV7+nQpGmgcg 4Rtm0xXSLpE7Cqlr1IuBAncngI4xpQMZ83DZ7J1kqn+xJlKqTLYiQU9Tbvi7x0Qijb/U Oq2d+QhmHi5W061zLpbUM5G4imdoMuJAhFeXgiMNcCXN0Gh+fWR6vn2xZyByTYti+t6B gGEGnECniYp4DZdcM+gyEwR9i8+zP04xsqEd4xDQ+5KQTN0REoST+NhljaA3+o9/xzCH 1D+jfBHCYE8QLmbuKSajKJHk3pj6nNl3ktTG15sc7fbJupnVThGRrGEJHZVZyA5MWVf8 ZkHQ== MIME-Version: 1.0 In-Reply-To: <20121011081607.46014e3d@dilbert> References: <20121011081607.46014e3d@dilbert> Date: Fri, 12 Oct 2012 00:14:11 +1100 Subject: Re: for-loop on cmd-line From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349961261 news.xs4all.nl 6877 [2001:888:2000:d::a6]:34208 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31113 On Thu, Oct 11, 2012 at 11:16 PM, D'Arcy J.M. Cain wrote: > On Thu, 11 Oct 2012 13:24:22 +0200 > 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): > > Is "one liner" an actual requirement or is the requirement to run it > from the command line? > > python -c " > import sys > i = 0 > for p in sys.path: > print('sys.path[%2d]: %s' % (i, p)) > i+=1 > " > > I don't know if this works on Windows or not. It doesn't, I just tested it. Windows batch is appallingly crude compared to a modern Unix shell; you may be able to find a way to get around this, but the easiest solution for most batch files is going to be an actual Python script file. You may be able to overlay your batch and Python scripts with a trick like this: rem = ''' @echo off echo This is batch \python32\python %0 echo All done exit /b rem ''' import sys print("This is Python") for i,p in enumerate(sys.path): print('sys.path[%2d]: %s' % (i, p)) print("Python done") You'll have a variable in Python called 'rem' which contains all your batch code :) It exploits the fact that 'rem' makes a one-line comment, but the triple quotes go across multiple lines. (The "exit /b" should exit the batch script without closing cmd.exe - this is yet another weird WEIRD wart in Windows batch. I'm pretty sure neither DOS nor OS/2 batch required that parameter.) ChrisA