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
40k7 gold badges94 silver badges124 bronze badges
asked Jan 29, 2017 at 5:53
1
  • 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. Commented Jan 29, 2017 at 22:04

1 Answer 1

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.

answered Jan 29, 2017 at 6:01
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.