【问题标题】:How can I recognise the argv correctly in Python cmd?如何在 Python cmd 中正确识别 argv?
【发布时间】:2014-03-04 02:44:04
【问题描述】:

这是代码:

import sys
# Gather our code in a main() function

def main():
    for arg in sys.argv:
        print(arg)

# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
    main()

当我像a.py a&7 这样在cmd 中运行它时,它只打印a&7 无法识别)。 如何将& 之类的字符发送到sys.argv[]

【问题讨论】:

  • 包含您已经尝试过的内容有时会有所帮助。
  • 我只是不知道t know how to solve this kind of problem,because google seems wont 识别特殊字符,例如'&'。

标签: python shell command-line-arguments


【解决方案1】:

在 Windows 上,使用 ^ 转义 & 符号:

python a.py a^&7

或者用双引号引用(单引号不行):

python a.py "a&7"

【讨论】:

    【解决方案2】:

    假设这是一个类 Unix 系统,您需要引用参数以防止 shell 解析它并将& 视为命令分隔符:

    a.py 'a&7'
    

    【讨论】:

    • @mascure:在 Windows 上引用命令行参数仍然是个好主意。
    猜你喜欢
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多