终端:
python test.py blah='blah'
在test.py中
print sys.argv
['test.py', 'blah=blah'] <------------
如何保留blah参数的“”或''?
有没有一种方法可以知道参数是否用“”或''包装?
python test.py "blah='blah'"
也可以放置在参数的任何位置:
python test.py blah="'blah'"
或者您可以使用反斜杠转义:
python test.py blah=\'blah\'
为了保存它们,这取决于你用来运行命令的确切shell。
bash
示例:
$ cat test.py
import sys
print sys.argv
$ python test.py blah='blah'
['test.py', 'blah=blah']
$ python test.py "blah='blah'"
['test.py', "blah='blah'"]
$ python test.py blah="'blah'"
['test.py', "blah='blah'"]
$ python test.py blah=\'blah\'
['test.py', "blah='blah'"]
cmd
控制台引用使用不同于bash
shell的规则。我对tsh
或zsh
的经验很少,但它们可能会再次使用不同的规则。 - Martijn Pieters或许
python test.py blah="'blah'"
python test.py "blah='blah'"
。 - Gumbo