Skip to content Skip to sidebar Skip to footer

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 fo

Solution 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.

Post a Comment for "Python Command Line -x Option"