终端:
python test.py blah='blah'
在测试.py中
print sys.argv
['test.py', 'blah=blah'] <------------
blah arg 如何保留其 '' 或
有没有办法知道 arg 是否用“”或“”包裹?
Your shell在调用 Python 之前删除引号。这不是Python可以控制的。
添加更多报价:
python test.py "blah='blah'"
它也可以放在参数中的任何位置:
python test.py blah="'blah'"
或者你可以使用反斜杠转义:
python test.py blah=\'blah\'
来保护它们。这确实取决于您用来运行命令的确切 shell。
Demo on 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'"]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)