Python 命令行 -x 选项 [英] Python command line -x option
问题描述
我最近看到 python 将 -x 作为命令行选项,它在此处的文档中解释了这一点
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.
但是,请原谅我 :) 我真的不明白为什么有人会使用 #!cmd 的非 Unix 形式,即使使用了(对于 DOS)为什么要跳过它?
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 ?
推荐答案
使用以下第一行启动 Windows 批处理脚本 (.bat
/.cmd
):>
Start a Windows batch script (.bat
/.cmd
) with this first line:
@echo off & python -x "%~f0" %* & goto :eof
当您运行它时,cmd.exe
将静默运行 python -x
并带有脚本的完整文件路径和其余的命令行参数,然后忽略文件的其余部分.python
将读取文件,跳过第一行(不会解析为 Python 代码),并将其余部分视为普通 Python 脚本.
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.
这在类 UNIX 平台上不是必需的,因为您会放置
This isn't necessary on UNIX-like platforms because you'd put
#!/usr/bin/env python
在脚本的顶部获得类似的行为,这看起来像是对 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.
这篇关于Python 命令行 -x 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!