1

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 ?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • 1
    Note 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 Jan 29 '17 at 22:04

1 Answers1

9

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.

ephemient
  • 198,619
  • 38
  • 280
  • 391