redirecting stdin with Python scripts under win2k (solution)

Bengt Richter bokr at oz.net
Thu Mar 14 15:24:58 EST 2002


On Thu, 14 Mar 2002 11:38:52 +0000, Dale Strickland-Clark <dale at riverhall.NOTHANKS.co.uk> wrote:

>I'm sure you're very pleased with that but I don't see the point.
>
>If I want to run a Python program, I just type the name of it at a
>command prompt.
>
Yes, but that is not the key point of Syver's contribution.

>A few prerequisites need to be in place but some of these happen
>during install:
>
>1. The directory containing the script needs to be in your PATH
>environment var or be the current directory.
>2. PATHEXT environment var needs to contain ".PY;.PYC;.PYW"
>3. The Python exes need to be associated with the Python file types.
>
>Once you get all the above right, you just type:
>
>C:\> wibble args
>
>and it runs:
>
>"C:\Python21\python.exe" "wibble.py" args
>
You are missing the key point, which is about redirection of i/o.
If you tried to use your wibble thus:

    wibble args > outputfile

You would get a zero length output file on NT4.
Syver solved _that_ problem without using a separate wrapper.

I would have been pleased to have thought of it, especially since
I recently posted separate wrapping solutions. Plus, not long ago I
saw a post using a similar technique with a linux shell in a cgi
context, and I didn't make the connection. Syver either re-invented
that (for windows) or made the connection and reformulated it. Either
way, I think he deserves credit, not a comment like "I'm sure you're
very pleased with that ..."

>This only works with Win NT/2K/XP. As for 95/98/ME, well it's easier
>not to bother, really.
>
>Disguising your Python programs as batch files is going to knacker any
>attempt your editor may make to help with syntax highlighting and
>suchlike.
That may be a downside for editors that decide on the basis of file extension,
but you are not obligated to do all your scripts this way ;-)

Being able to invoke a script concisely and have its output
redirectable is something you want for full flexibility e.g. in putting
customized scripted build steps in the VC++ IDE for e.g., customizing
building Python from source on windows.

Being able to wrap without a separate file is a good thing IMO, although
for scripts that are supposed to be verbatim (e.g. signed etc) portable to
*nix, you'd want to continue using separate wrappers in windows.

<interesting side note>
If you use a hash, e.g. MD5, or PGP to sign/validate a script, should it
be based on canonicalizing to unix format (i.e., \n vs \r\n or \r)?
I imagine the XML folks must have decided something. ...but I don't have
time to google every stray question that pops to mind ;-)
</interesting side note>

>
>Also, FYI, I think all the command extensions you mention were
>introduced with NT 4.
Yes, the technique works on NT4, which pleases me ;-)
The cmd.exe batch command line syntax is really horrid, weak, and badly
documented though, so it's nice to be able to transition to Python in one line!

Regards,
Bengt Richter



More information about the Python-list mailing list