[Tutor] About the vertical bar input

Dave Angel davea at ieee.org
Mon Jun 29 22:07:56 CEST 2009


"hyou"  wrote:

> Hello,
>
>  
>
> I'm trying to write a script that simply execute a command line like:
>
> C:\...(path)..\Devenv solution /build "Debug|Win32"
>
>  
>
> However, in Python the "|" symbol is reserved thus I just can't make the
> command line above working once I added the "|" sign in it.
>
>  
>
> How can I put the "original" vertical bar in a string?
>
>  
>
> Thanks!
>
>   

It would help if you mentioned which version of python you're using, and 
in what OS environment.  I can guess you're running some Windows version 
because of the C:\ but the problem could also be different between XP 
and Vista, and certainly different between Windows95 and Windows 7.

It also would help if you actually showed what you tried, and what the 
response was.  Cut and paste the command window text into your message.

I'm going to guess you did not mean "write a script that simply execute 
a command line like" but instead meant that the command line would be 
entered in a batch file, or at the command prompt, and that the python 
script was the first argument to the command line.  In your case, the 
script is called "Devenv.py" and that you replaced most of its path with 
ellipses.

As far as I know, the vertical bar isn't a special character in Python 
at all.  It is, however, special in CMD.EXE, the command interpreter for 
recent Windows versions.  It is used to specify multiple programs on the 
same command line, where the stdout of the first is piped into the stdin 
of the next.  Similarly, the > and < symbols are used to specify 
redirection, and the & symbol is used to separate two commands that are 
to be run sequentially.

CMD.EXE isn't the only shell available for Windows, but it's the 
standard one built into all the recent versions.  So I'm guessing that's 
what you're using.  If you're running a different one, such as 4NT, let 
us know.

In some versions of Windows, for example in XP, the | character is not 
special inside a quoted string.  So the example you sort-of supplied 
would have no problem.

To demonstrate,  I created a trivial Python script  echo2.py:

import sys
print "Args are ---", sys.argv, "---"

Then the following is pasted from a CMD window:

M:\Programming\Python\sources\dummy>echo2 How are you?
Args are --- ['M:\\Programming\\Python\\sources\\dummy\\echo2.py', 
'How', 'are',
 'you?'] ---

M:\Programming\Python\sources\dummy>echo2 This has a "vertical|bar"
Args are --- ['M:\\Programming\\Python\\sources\\dummy\\echo2.py', 
'This', 'has'
, 'a', 'vertical|bar'] ---

M:\Programming\Python\sources\dummy>echo2 This is not|quoted
'quoted' is not recognized as an internal or external command,
operable program or batch file.

Note that if you cannot do matching quotes, you can get some funny 
groupings of text.  For example, if you have leading quotes but no 
trailing quotes, then the entire rest of the line will be one element of 
sys.argv.

DaveA



More information about the Tutor mailing list