《Python基础教程里的例子》
#tidy.py
from subprocess import Popen,PIPE
text=open('messy.html').read()
tidy=Popen('tidy',stdin=PIPE,stdout=PIPE,stderr=PIPE)
tidy.stdin.write(text)
tidy.stdin.close()
print tidy.stdout.read()
运行时报错:
root@ubuntu:~/python# python tidy.py
Traceback (most recent call last):
File "tidy.py", line 4, in <module>
tidy=Popen('tidy',stdin=PIPE,stdout=PIPE,stderr=PIPE)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
解决方法:
tidy=Popen('tidy',stdin=PIPE,stdout=PIPE,stderr=PIPE,shell=True)
在参数中添加了shell=True。
本文介绍了一个Python脚本tidy.py,在使用subprocess模块调用外部程序tidy时遇到的问题及解决办法。通过在Popen函数中添加shell=True参数解决了找不到tidy命令的问题。
2124

被折叠的 条评论
为什么被折叠?



