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


Groups > comp.lang.python > #31115

Re: for-loop on cmd-line

Newsgroups comp.lang.python
Date 2012-10-11 06:16 -0700
References <E7295A9F2EFF4FD0A64C1225370AAC56@dev.null> <20121011081607.46014e3d@dilbert> <mailman.2047.1349961261.27098.python-list@python.org>
Subject Re: for-loop on cmd-line
From Ramchandra Apte <maniandram01@gmail.com>
Message-ID <mailman.2048.1349961393.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Thursday, 11 October 2012 18:44:44 UTC+5:30, Chris Angelico  wrote:
> On Thu, Oct 11, 2012 at 11:16 PM, D'Arcy J.M. Cain <darcy@druid.net> wrote:
> 
> > On Thu, 11 Oct 2012 13:24:22 +0200
> 
> > Gisle Vanem <gvanem@broadpark.no> 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

What about the "Power" in PowerShell?

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


Thread

Re: for-loop on cmd-line Chris Angelico <rosuav@gmail.com> - 2012-10-12 00:14 +1100
  Re: for-loop on cmd-line Ramchandra Apte <maniandram01@gmail.com> - 2012-10-11 06:16 -0700
    Re: for-loop on cmd-line Chris Angelico <rosuav@gmail.com> - 2012-10-12 00:20 +1100
  Re: for-loop on cmd-line Ramchandra Apte <maniandram01@gmail.com> - 2012-10-11 06:16 -0700
    Re: for-loop on cmd-line wxjmfauth@gmail.com - 2012-10-11 09:24 -0700
      Re: for-loop on cmd-line Chris Angelico <rosuav@gmail.com> - 2012-10-12 03:32 +1100
      Re: for-loop on cmd-line Gisle Vanem <gvanem@broadpark.no> - 2012-10-11 18:49 +0200
      Re: for-loop on cmd-line Chris Angelico <rosuav@gmail.com> - 2012-10-12 03:58 +1100
      RE: for-loop on cmd-line "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-11 20:12 +0000
    Re: for-loop on cmd-line wxjmfauth@gmail.com - 2012-10-11 09:24 -0700

csiph-web