TypeError仅在启动进程多次执行后发生

时间:2013-09-24 09:22:37

标签: python debugging process

我似乎有一个非常奇怪的错误。以下代码可以工作两到三次,然后以TypeError

结束
cmd = "perl prog.pl"
process = Popen(shlex.split(cmd), stdout=PIPE)
matchstr=process.communicate()
exit_code = process.wait()
print exit_code 

embedded_rawstr = r"""Extract"(?P<ex>.*?)".*?"""
print re.findall(embedded_rawstr, matchstr)

代码在循环中调用,因此它经常被调用。为什么会出现此错误以及如何解决?

Traceback (most recent call last):
  File "ga-test.py", line 122, in <module>
    main()
  File "ga-test.py", line 65, in main
    fitnesses = list(map(toolbox.evaluate, pop))
  File "ga-test.py", line 47, in evalOneMax
    print re.findall(embedded_rawstr, matchstr)
  File "C:\Python27\lib\re.py", line 177, in findall
    return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer

1 个答案:

答案 0 :(得分:0)

Popen.communicate()返回一个元组(stdoutdata,stderrdata)。

替换以下内容:

matchstr = process.communicate()

使用:

matchstr, stderr = process.communicate()