redirecting stdin with Python scripts under win2k (solution)

Syver Enstad syver-en+usenet at online.no
Wed Mar 13 20:07:30 EST 2002


It seems that I've finally found an elegant way to do #! functionality on win2k
and upwards:

instead of #! ... 

put:

@python -x "%~f0" %* & goto :EOF

in the first line of your python script file.

Call the file *scriptname*.bat

If this file is located in your path, you should be to call it from
anywhere by writing on the commandline:

*scriptname*

or:

*scriptname*.bat

Redirection of iostreams and piping between programs should work as
expected, and not fail as it does when using .py files on the
commandline.


Notes:
for emacs users put this in the bottom of your scriptfile.
# Local Variables: ***
# mode:python ***
# End: ***

Explanation of commandline:
@python      -x                              "%~f0" %* 
(run python) (skip first line of sourcetext) from fully qualified name of
the file itself)

&                        goto :EOF
command separator        jump to end of file 


This commanline uses the command extensions that are provided in
win2000 so it won't work on win9x family of win32 operating systems. I
don't think they work in NT 4 either but I am not sure.


-- 

Vennlig hilsen 

Syver Enstad



More information about the Python-list mailing list