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


Groups > comp.lang.python > #31113 > unrolled thread

Re: for-loop on cmd-line

Started byChris Angelico <rosuav@gmail.com>
First post2012-10-12 00:14 +1100
Last post2012-10-11 09:24 -0700
Articles 10 — 5 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#31113 — Re: for-loop on cmd-line

FromChris Angelico <rosuav@gmail.com>
Date2012-10-12 00:14 +1100
SubjectRe: for-loop on cmd-line
Message-ID<mailman.2047.1349961261.27098.python-list@python.org>
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

[toc] | [next] | [standalone]


#31114

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-10-11 06:16 -0700
Message-ID<29d3c39a-ca37-403d-a9ed-dc1b3771d1f2@googlegroups.com>
In reply to#31113
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?

[toc] | [prev] | [next] | [standalone]


#31118

FromChris Angelico <rosuav@gmail.com>
Date2012-10-12 00:20 +1100
Message-ID<mailman.2050.1349961626.27098.python-list@python.org>
In reply to#31114
On Fri, Oct 12, 2012 at 12:16 AM, Ramchandra Apte
<maniandram01@gmail.com> wrote:
> What about the "Power" in PowerShell?

What about it? Are you suggesting that the OP use it? Are you saying
that Windows batch already includes it? You quoted my entire post
(double-spaced), but that context adds nothing to your statement; it
still stands alone as a complete non sequitur.

And you're posting to both c.l.p and p-l...

ChrisA

[toc] | [prev] | [next] | [standalone]


#31115

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-10-11 06:16 -0700
Message-ID<mailman.2048.1349961393.27098.python-list@python.org>
In reply to#31113
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?

[toc] | [prev] | [next] | [standalone]


#31124

Fromwxjmfauth@gmail.com
Date2012-10-11 09:24 -0700
Message-ID<029d1272-68a7-4313-92f7-760e5bec141d@googlegroups.com>
In reply to#31115
Le jeudi 11 octobre 2012 15:16:33 UTC+2, Ramchandra Apte a écrit :

PS C:\> $cmd="import sys;"
PS C:\> $cmd+="print('\n'.join(sys.path))"
PS C:\> $cmd
import sys;print('\n'.join(sys.path))
PS C:\> c:\python32\python -c $cmd

C:\Windows\system32\python32.zip
c:\python32\DLLs
c:\python32\lib
c:\python32
c:\python32\lib\site-packages
PS C:\>

Can probably be in a .cmd file.

jmf

[toc] | [prev] | [next] | [standalone]


#31126

FromChris Angelico <rosuav@gmail.com>
Date2012-10-12 03:32 +1100
Message-ID<mailman.2056.1349973166.27098.python-list@python.org>
In reply to#31124
On Fri, Oct 12, 2012 at 3:24 AM,  <wxjmfauth@gmail.com> wrote:
> Le jeudi 11 octobre 2012 15:16:33 UTC+2, Ramchandra Apte a écrit :
>
> PS C:\> $cmd="import sys;"
> PS C:\> $cmd+="print('\n'.join(sys.path))"
> PS C:\> $cmd
> import sys;print('\n'.join(sys.path))
> PS C:\> c:\python32\python -c $cmd
>
> C:\Windows\system32\python32.zip
> c:\python32\DLLs
> c:\python32\lib
> c:\python32
> c:\python32\lib\site-packages
> PS C:\>
>
> Can probably be in a .cmd file.

That doesn't actually make a multi-line argument though. It just
dodges the issue by avoiding the for loop :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#31127

FromGisle Vanem <gvanem@broadpark.no>
Date2012-10-11 18:49 +0200
Message-ID<mailman.2057.1349974170.27098.python-list@python.org>
In reply to#31124
<wxjmfauth@gmail.com> wrote in comp.lang.python

(my ISP no longer updates this group. Last message is from 8. April.
 Does the postings to the python mailing-list automatically get reposted 
 to comp.lang.python?)

> C:\Windows\system32\python32.zip
> c:\python32\DLLs

I see a similar result:
  f:\Windows\system32\python27.zip

Where is it determined that python27.zip should be in sys.path?
I have no such file anywhere. I'm using ActivePython 2.7.2.

--gv

[toc] | [prev] | [next] | [standalone]


#31128

FromChris Angelico <rosuav@gmail.com>
Date2012-10-12 03:58 +1100
Message-ID<mailman.2060.1349974728.27098.python-list@python.org>
In reply to#31124
On Fri, Oct 12, 2012 at 3:49 AM, Gisle Vanem <gvanem@broadpark.no> wrote:
> <wxjmfauth@gmail.com> wrote in comp.lang.python
>
> (my ISP no longer updates this group. Last message is from 8. April.
> Does the postings to the python mailing-list automatically get reposted to
> comp.lang.python?)

Yes, c.l.p and python-list mirror each other.

>> C:\Windows\system32\python32.zip
>> c:\python32\DLLs
>
>
> I see a similar result:
>  f:\Windows\system32\python27.zip
>
> Where is it determined that python27.zip should be in sys.path?
> I have no such file anywhere. I'm using ActivePython 2.7.2.

It's in sys.path in the three Windows Pythons I have here:

C:\Documents and Settings\M>python -c "import sys; print(sys.version); print('\n
'.join(sys.path))"
2.4.5 (#1, Jul 22 2011, 02:01:04)
[GCC 4.1.1]

C:\Program Files\LilyPond\usr\lib\python24.zip
C:\Program Files\LilyPond\usr\lib\python2.4
C:\Program Files\LilyPond\usr\lib\python2.4\plat-mingw32
C:\Program Files\LilyPond\usr\lib\python2.4\lib-tk
C:\Program Files\LilyPond\usr\lib\python2.4\lib-dynload
C:\Program Files\LilyPond\usr\lib\python2.4\site-packages

C:\Documents and Settings\M>\python26\python -c "import sys; print(sys.version);
 print('\n'.join(sys.path))"
2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)]

C:\WINDOWS\system32\python26.zip
C:\python26\DLLs
C:\python26\lib
C:\python26\lib\plat-win
C:\python26\lib\lib-tk
C:\python26
C:\python26\lib\site-packages

C:\Documents and Settings\M>\python32\python -c "import sys; print(sys.version);
 print('\n'.join(sys.path))"
3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)]

C:\WINDOWS\system32\python32.zip
C:\python32\DLLs
C:\python32\lib
C:\python32
C:\python32\lib\site-packages
C:\python32\lib\site-packages\win32
C:\python32\lib\site-packages\win32\lib
C:\python32\lib\site-packages\Pythonwin

C:\Documents and Settings\M>

Presumably it's so that I can zip up my entire Python library and toss
it into a convenient file. I don't think it costs much to stat a file
and find it's not there before moving on, so it's not a problem to
leave it there.

ChrisA

[toc] | [prev] | [next] | [standalone]


#31130

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-10-11 20:12 +0000
Message-ID<mailman.2063.1349986347.27098.python-list@python.org>
In reply to#31124
Chris Angelico wrote:
> On Fri, Oct 12, 2012 at 3:49 AM, Gisle Vanem <gvanem@broadpark.no> wrote:
> > <wxjmfauth@gmail.com> wrote in comp.lang.python
> >
> > (my ISP no longer updates this group. Last message is from 8. April.
> > Does the postings to the python mailing-list automatically get reposted to
> > comp.lang.python?)
> 
> Yes, c.l.p and python-list mirror each other.
> 
> >> C:\Windows\system32\python32.zip
> >> c:\python32\DLLs
> >
> >
> > I see a similar result:
> >  f:\Windows\system32\python27.zip
> >
> > Where is it determined that python27.zip should be in sys.path?
> > I have no such file anywhere. I'm using ActivePython 2.7.2.
> 
> It's in sys.path in the three Windows Pythons I have here:
> 
> C:\Documents and Settings\M>python -c "import sys; print(sys.version); print('\n
> '.join(sys.path))"
> 2.4.5 (#1, Jul 22 2011, 02:01:04)
> [GCC 4.1.1]
> 
> C:\Program Files\LilyPond\usr\lib\python24.zip
> C:\Program Files\LilyPond\usr\lib\python2.4
> C:\Program Files\LilyPond\usr\lib\python2.4\plat-mingw32
> C:\Program Files\LilyPond\usr\lib\python2.4\lib-tk
> C:\Program Files\LilyPond\usr\lib\python2.4\lib-dynload
> C:\Program Files\LilyPond\usr\lib\python2.4\site-packages
> 
> C:\Documents and Settings\M>\python26\python -c "import sys; print(sys.version);
>  print('\n'.join(sys.path))"
> 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)]
> 
> C:\WINDOWS\system32\python26.zip
> C:\python26\DLLs
> C:\python26\lib
> C:\python26\lib\plat-win
> C:\python26\lib\lib-tk
> C:\python26
> C:\python26\lib\site-packages
> 
> C:\Documents and Settings\M>\python32\python -c "import sys; print(sys.version);
>  print('\n'.join(sys.path))"
> 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)]
> 
> C:\WINDOWS\system32\python32.zip
> C:\python32\DLLs
> C:\python32\lib
> C:\python32
> C:\python32\lib\site-packages
> C:\python32\lib\site-packages\win32
> C:\python32\lib\site-packages\win32\lib
> C:\python32\lib\site-packages\Pythonwin
> 
> C:\Documents and Settings\M>
> 
> Presumably it's so that I can zip up my entire Python library and toss
> it into a convenient file. I don't think it costs much to stat a file
> and find it's not there before moving on, so it's not a problem to
> leave it there.
> 
> ChrisA

Interesting, my results are slightly different. Here is what I 
get from (one of) my Python installs.

2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)]
C:\ramit\Python27\python27.zip
C:\ramit\Python27\DLLs
C:\ramit\Python27\lib
C:\ramit\Python27\lib\plat-win
C:\ramit\Python27\lib\lib-tk
C:\ramit\Python27
C:\ramit\Python27\lib\site-packages

Ramit

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [prev] | [next] | [standalone]


#31125

Fromwxjmfauth@gmail.com
Date2012-10-11 09:24 -0700
Message-ID<mailman.2055.1349972653.27098.python-list@python.org>
In reply to#31115
Le jeudi 11 octobre 2012 15:16:33 UTC+2, Ramchandra Apte a écrit :

PS C:\> $cmd="import sys;"
PS C:\> $cmd+="print('\n'.join(sys.path))"
PS C:\> $cmd
import sys;print('\n'.join(sys.path))
PS C:\> c:\python32\python -c $cmd

C:\Windows\system32\python32.zip
c:\python32\DLLs
c:\python32\lib
c:\python32
c:\python32\lib\site-packages
PS C:\>

Can probably be in a .cmd file.

jmf

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web