Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
for-loop on cmd-line
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  17 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Gisle Vanem  
View profile  
 More options Oct 11 2012, 7:24 am
Newsgroups: comp.lang.python
From: Gisle Vanem <gva...@broadpark.no>
Date: Thu, 11 Oct 2012 13:24:22 +0200
Local: Thurs, Oct 11 2012 7:24 am
Subject: for-loop on cmd-line
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
suzaku  
View profile  
 More options Oct 11 2012, 7:50 am
Newsgroups: comp.lang.python
From: suzaku <satorulo...@gmail.com>
Date: Thu, 11 Oct 2012 04:50:57 -0700 (PDT)
Local: Thurs, Oct 11 2012 7:50 am
Subject: Re: for-loop on cmd-line
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):


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Oct 11 2012, 7:55 am
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Thu, 11 Oct 2012 07:54:33 -0400
Local: Thurs, Oct 11 2012 7:54 am
Subject: Re: for-loop on cmd-line
On 10/11/2012 07:24 AM, Gisle Vanem wrote:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
D'Arcy J.M. Cain  
View profile  
 More options Oct 11 2012, 8:25 am
Newsgroups: comp.lang.python
From: "D'Arcy J.M. Cain" <da...@druid.net>
Date: Thu, 11 Oct 2012 08:16:07 -0400
Local: Thurs, Oct 11 2012 8:16 am
Subject: Re: for-loop on cmd-line
On Thu, 11 Oct 2012 13:24:22 +0200

Gisle Vanem <gva...@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.

--
D'Arcy J.M. Cain <da...@druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.
IM: da...@Vex.Net


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 11 2012, 9:14 am
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Fri, 12 Oct 2012 00:14:11 +1100
Local: Thurs, Oct 11 2012 9:14 am
Subject: Re: for-loop on cmd-line
On Thu, Oct 11, 2012 at 11:16 PM, D'Arcy J.M. Cain <da...@druid.net> wrote:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ramchandra Apte  
View profile  
 More options Oct 11 2012, 9:16 am
Newsgroups: comp.lang.python
From: Ramchandra Apte <maniandra...@gmail.com>
Date: Thu, 11 Oct 2012 06:16:25 -0700 (PDT)
Local: Thurs, Oct 11 2012 9:16 am
Subject: Re: for-loop on cmd-line

What about the "Power" in PowerShell?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ramchandra Apte  
View profile  
 More options Oct 11 2012, 9:16 am
Newsgroups: comp.lang.python
From: Ramchandra Apte <maniandra...@gmail.com>
Date: Thu, 11 Oct 2012 06:16:25 -0700 (PDT)
Local: Thurs, Oct 11 2012 9:16 am
Subject: Re: for-loop on cmd-line

What about the "Power" in PowerShell?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 11 2012, 9:20 am
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Fri, 12 Oct 2012 00:20:23 +1100
Local: Thurs, Oct 11 2012 9:20 am
Subject: Re: for-loop on cmd-line
On Fri, Oct 12, 2012 at 12:16 AM, Ramchandra Apte

<maniandra...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gisle Vanem  
View profile  
 More options Oct 11 2012, 9:41 am
Newsgroups: comp.lang.python
From: Gisle Vanem <gva...@broadpark.no>
Date: Thu, 11 Oct 2012 15:40:57 +0200
Local: Thurs, Oct 11 2012 9:40 am
Subject: Re: for-loop on cmd-line

"Dave Angel" <d...@davea.name> wrote:
> 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.

Ok, I suspected something like that.

> You can do it easily enough with a list comprehension.  Let us know if
> you can't work that out.

Later. I'm only scratching the surface of Python.

> Any reason why you don't just make a one-file python script, and run
> that instead of your one line batch file?

I though of calling that python line from a C-program using
popen() and parsing the output. Since popen() on Win32 AFAIK doesn't accept
multiple lines, I guess I must write a .py-file to %TEMP first.

Thank to all.

--gv


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
wxjmfa...@gmail.com  
View profile  
 More options Oct 11 2012, 12:24 pm
Newsgroups: comp.lang.python
From: wxjmfa...@gmail.com
Date: Thu, 11 Oct 2012 09:24:10 -0700 (PDT)
Local: Thurs, Oct 11 2012 12:24 pm
Subject: Re: for-loop on cmd-line
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
wxjmfa...@gmail.com  
View profile  
 More options Oct 11 2012, 12:24 pm
Newsgroups: comp.lang.python
From: wxjmfa...@gmail.com
Date: Thu, 11 Oct 2012 09:24:10 -0700 (PDT)
Local: Thurs, Oct 11 2012 12:24 pm
Subject: Re: for-loop on cmd-line
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 11 2012, 12:33 pm
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Fri, 12 Oct 2012 03:32:43 +1100
Local: Thurs, Oct 11 2012 12:32 pm
Subject: Re: for-loop on cmd-line

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

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gisle Vanem  
View profile  
 More options Oct 11 2012, 12:49 pm
Newsgroups: comp.lang.python
From: Gisle Vanem <gva...@broadpark.no>
Date: Thu, 11 Oct 2012 18:49:27 +0200
Local: Thurs, Oct 11 2012 12:49 pm
Subject: Re: for-loop on cmd-line
<wxjmfa...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 11 2012, 12:58 pm
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Fri, 12 Oct 2012 03:58:45 +1100
Local: Thurs, Oct 11 2012 12:58 pm
Subject: Re: for-loop on cmd-line

On Fri, Oct 12, 2012 at 3:49 AM, Gisle Vanem <gva...@broadpark.no> wrote:
> <wxjmfa...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Prasad, Ramit  
View profile  
 More options Oct 11 2012, 4:12 pm
Newsgroups: comp.lang.python
From: "Prasad, Ramit" <ramit.pra...@jpmorgan.com>
Date: Thu, 11 Oct 2012 20:12:06 +0000
Local: Thurs, Oct 11 2012 4:12 pm
Subject: RE: for-loop on cmd-line

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.  


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Oct 11 2012, 4:41 pm
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Thu, 11 Oct 2012 16:40:55 -0400
Local: Thurs, Oct 11 2012 4:40 pm
Subject: Re: for-loop on cmd-line
On 10/11/2012 09:40 AM, Gisle Vanem wrote:

Why would you write some C-program just to save having two separate
files, one batch and one for the script?  For that matter, several
answers have given you approaches that didn't involve list
comprehensions, including merging the two in a single file, using an
initial variable of rem="""

What are your real constraints?  Are you just playing code-golf?

--

DaveA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gisle Vanem  
View profile  
 More options Oct 11 2012, 5:00 pm
Newsgroups: comp.lang.python
From: Gisle Vanem <gva...@broadpark.no>
Date: Thu, 11 Oct 2012 23:00:09 +0200
Local: Thurs, Oct 11 2012 5:00 pm
Subject: Re: for-loop on cmd-line

"Dave Angel" <d...@davea.name> wrote:
> Why would you write some C-program just to save having two separate
> files, one batch and one for the script?  For that matter, several
> answers have given you approaches that didn't involve list
> comprehensions, including merging the two in a single file, using an
> initial variable of rem="""

Like I wrote; use popen() or system() from a C-program (an env-var
checker) that's not really related to Python programming. But rather to
check various stuff needed for C-programming . Like walking the list
of %INCLUDE / %C_INCLUDE_PATH dirs to figure out what headers are
where. So I'd just as well add an option to check for Python paths too
(if Python is installed that is).

> What are your real constraints?  Are you just playing code-golf?

That too.

--gv


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »