[英]Getting a list by sys.argv
I pass mylist as an argument to be get after by sys.argv I do this: 我将mylist作为要通过sys.argv获取的参数传递给我,方法是:
mylist = str(list)
nbre = str(nbre)
comm = 'python2.6 file.py ' + mylist + ' ' + nbre + ' &'
os.system(comm)
In file.py
, I am expected to get mylist
by this way and which contains [machine1,machine2]
but when doing: 在file.py
,我希望通过这种方式获取mylist
,其中包含[machine1,machine2]
但是在执行以下操作时:
mylist = sys.argv[1]
I get [machine1
, which is wrong. 我得到[machine1
,这是错误的。 When I display sys.argv
I found: 当我显示sys.argv
我发现:
['file.py','[machine1,','machine2]','1']
I didn't understand why my list is composed like that? 我不明白为什么我的名单是这样组成的?
Apart from this being a terrible way to communicate a list from one python script to another, you'd need to use quotes around the list to prevent it from being split by the shell: 除了这是一种将列表从一个python脚本传递到另一个python脚本的可怕方式之外,您还需要在列表周围使用引号,以防止它被shell分割:
comm = 'python2.6 file.py "%s" "%s" &' % (mylist, nbre)
I've used string formatting to put the quotes around mylist
and nbre
. 我已经使用字符串格式将引号放在mylist
和nbre
周围。
You really want to look into the subprocess
module to invoke other processes without the shell getting in the way. 您确实想研究subprocess
模块以调用其他流程,而不会影响外壳。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.