使用Popen时报错:OSError: [Errno 2] No such file or directory

版权声明:本文转载自 张晨的专栏(http://blog.csdn.net/zc123456zzc), 转载请保留本声明! https://blog.csdn.net/zc123456zzc/article/details/48499035

《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

猜你喜欢

转载自blog.csdn.net/zc123456zzc/article/details/48499035