2013-09-24 25 views
0

我似乎有一个非常奇怪的错误。下面的代码工作两三次,然后用一个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

添加了错误跟踪 – tarrasch

+0

我怀疑来自该过程的matchstr是空的。这怎么会发生? – tarrasch

回答

0

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

替换以下:

matchstr = process.communicate() 

有:

matchstr, stderr = process.communicate()