2013-03-25 56 views
-1

这是代码:如何在Python cmd中正确识别argv?

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

+0

包含您已经尝试过的内容有时很有帮助。 – 2013-03-28 01:51:46

+0

我只是不知道如何解决这类问题,因为谷歌似乎不会识别'&'等特殊字符。 – mascure 2013-04-01 05:36:30

回答

3

在Windows上,躲避符号用^

python a.py a^&7 

或引用用双引号(单引号无效):

python a.py "a&7" 
+0

It works.Thanks。 – mascure 2013-03-25 10:36:12

2

假设这是一个类Unix系统,你需要引用参数来防止shell解析它和治疗&为命令分隔符:

a.py 'a&7' 
+0

在windows中如何? – mascure 2013-03-25 10:29:35

+0

@mascure:在Windows上引用命令行参数仍然是一个好主意。 – 2013-03-25 10:30:46