Here’s the bit of tricky batch file magic that does it:
1 2 3
@setlocal enabledelayedexpansion && python -x "%~f0" %* & exit /b !ERRORLEVEL! #start python code here print "hello world"
The way it works is that the first line of the file does two different things.
1. starts python interpreter passing the name of the file in, and the -x option will tell it to skip the first line (containing .bat file code)
2. When python finishes the script exits.
This nifty trick makes it much nicer for writing admin scripts with python on Windows.