如何在Python cmd中正确识别argv?
代码如下:如何在Python cmd中正确识别argv?,python,shell,command-line-arguments,Python,Shell,Command Line Arguments,代码如下: 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() 当我在cmd中像a.py a&7那样运行它时
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()
当我在cmd中像a.py a&7
那样运行它时,它只打印a
(&7
无法识别)。
如何将
&
等字符发送到sys.argv[]
?假设这是一个类似Unix的系统,您需要引用参数以防止shell解析它,并将&
视为命令分隔符:
a.py 'a&7'
在windows上,用
^
将符号转义为AND:
python a.py a^&7
或双引号报价(单引号无效):
@mascure:在Windows上引用命令行参数仍然是一个好主意。有时候,包含您已经尝试过的内容会很有帮助。我只是不知道如何解决这种问题,因为google似乎不会识别像“&”这样的特殊字符。
python a.py "a&7"