Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python command line -x option

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 ?


1 Answers

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.

like image 145
ephemient Avatar answered Sep 26 '26 05:09

ephemient



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!