I saw recently that python takes -x as a command line options which does what it explains in the docs here
python -x
Skip the first line of the source, allowing use of non-Unix forms of #!cmd. This is intended for a DOS specific hack only.
But, pardon me :) I don't really understand why someone would use a non-Unix form of #!cmd and even if it one does(for DOS) why skip it ?
-
1Note the documentation is output of date, CPython hasn't supported MS-DOS in very long time. This is actually a Window-specific hack.Ross Ridge– Ross Ridge2017年01月29日 22:04:58 +00:00Commented Jan 29, 2017 at 22:04
1 Answer 1
Start a Windows batch script (.bat/.cmd) with this first line:
@echo off & python -x "%~f0" %* & goto :eof
When you run it, cmd.exe will silently run python -x with the full file path to the script and the rest of the command-line arguments, then ignore the rest of the file. python will read the file, skipping the first line (which wouldn't parse as Python code), and treating the rest like a normal Python script.
This isn't necessary on UNIX-like platforms because you'd put
#!/usr/bin/env python
at the top of a script to get similar behavior, which looks like a comment to Python, so it continues executing through the rest of the file just fine.