Python3.5子进程错误
我使用下面的函数逐行读取python脚本输出并并行保存。但最终会出现回溯错误 代码:Python3.5子进程错误,python,logging,subprocess,python-3.5,Python,Logging,Subprocess,Python 3.5,我使用下面的函数逐行读取python脚本输出并并行保存。但最终会出现回溯错误 代码: def myrun(cmd): p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout = [] while True: line = p.stdout.readline() stdout.append(line)
def myrun(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = []
while True:
line = p.stdout.readline()
stdout.append(line)
print (line),
if line == '' and p.poll() != None:
break
return ''.join(stdout)
Traceback (most recent call last):
File "./Portability_Tests.py", line 38, in <module>
myrun(os.system("./tests_run.py"))
File "./Tests.py", line 11, in myrun
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "/usr/local/lib/python3.5/subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "/usr/local/lib/python3.5/subprocess.py", line 1171, in _execute_child
args = list(args)
TypeError: 'int' object is not iterable
当调用函数为:
myrun(os.system("./tests_run.py"))
我得到以下错误:
错误:
def myrun(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = []
while True:
line = p.stdout.readline()
stdout.append(line)
print (line),
if line == '' and p.poll() != None:
break
return ''.join(stdout)
Traceback (most recent call last):
File "./Portability_Tests.py", line 38, in <module>
myrun(os.system("./tests_run.py"))
File "./Tests.py", line 11, in myrun
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "/usr/local/lib/python3.5/subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "/usr/local/lib/python3.5/subprocess.py", line 1171, in _execute_child
args = list(args)
TypeError: 'int' object is not iterable
回溯(最近一次呼叫最后一次):
文件“/Portability_Tests.py”,第38行,在
myrun(操作系统(“./tests\u run.py”))
myrun中第11行的文件“/Tests.py”
p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.stdout)
文件“/usr/local/lib/python3.5/subprocess.py”,第676行,在__
恢复信号,启动新会话)
文件“/usr/local/lib/python3.5/subprocess.py”,第1171行,在执行子进程中
args=列表(args)
TypeError:“int”对象不可编辑
有人知道如何修复此错误吗 函数接收“程序参数序列或单个字符串”作为其args
参数
在
args
参数中传递的是os.system()
调用的输出,根据是“进程的退出状态”,因此是一个int
数字。相反,在cmd
变量中,您应该直接传递文件/tests\u run.py
的字符串(或其他迭代器)。如果您希望此路径与当前项目相关,则可以使用该模块。您不需要使用os.system
(顺便说一句,这是不安全的);尝试myrun(“./tests\u run.py”)
(假设命令正确)。我确实尝试了相同的操作,但没有帮助并抛出语法错误。我确实尝试直接调用“/tests\u run.py”但是thtows语法错误。你能发布回溯吗?没有回溯它一直打印b“”,直到我手动执行键盘中断b''b''b''b''b''b''b''^CTraceback(最近一次调用):文件“/tests_run.py”,第52行,在myrun(“/tests.py”)文件中“/tests_run.py”,第16行,在myrun打印(第行),键盘中断该错误与问题的原始错误无关。因为代码中有while True语句,所以您一直看到这个b。您需要修改它,以便脚本可以在某个点停止。但是,就这个线程而言,由于您成功访问了./tests.py文件,所以您可以接受答案并关闭它。