【发布时间】:2015-09-23 21:21:49
【问题描述】:
我正在将一个列表从subprocess 传递给父进程,并且在父进程中我想将它添加到一个已经存在的列表中。我这样做了:
subprocess_script.py:
def func():
list = []
list.append('1')
list.append('2')
print'Testing the list passing'
print '>>> list:',list
if __name__ == '__main__':
func()
parent_script.py:
list1 = []
list1.append('a')
list1.append('b')
ret = subprocess.Popen([sys.executable,"/Users/user1/home/subprocess_script.py"],stdout=subprocess.PIPE)
ret.wait()
return_code = ret.returncode
out, err = ret.communicate()
if out is not None:
for line in out.splitlines():
if not line.startswith('>>>'):
continue
value = line.split(':',1)[1].lstrip()
list1.extend(value)
print 'Final List: ',list1
但是当我执行这个时,我没有得到想要的输出。我想要的最终列表应该是:['a','b','1','2']。但我得到['a', 'b', '[', "'", '1', "'", ',', ' ', "'", '2', "'", ']'] 这是错误的。我在这里做错了什么?
【问题讨论】:
-
仅供参考,使用隐藏 Python 内置函数的变量名(此处为
list)并不是一个好习惯。
标签: python list subprocess output