如何在Python cmd中正确识别argv?

2024-05-15 02:35:57 发布

您现在位置:Python中文网/ 问答频道 /正文

代码如下:

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[]?在


Tags: theto代码inimportformaindef
2条回答

在windows上,用^转义与号:

python a.py a^&7

或用双引号引起来(单引号不起作用):

^{pr2}$

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

a.py 'a&7'

相关问题 更多 >

    热门问题