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

本文介绍了一个Python脚本tidy.py,在使用subprocess模块调用外部程序tidy时遇到的问题及解决办法。通过在Popen函数中添加shell=True参数解决了找不到tidy命令的问题。

《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

这个错误通常是因为系统无法找到 FFmpeg 可执行文件导致的。您需要确保已经正确安装了 FFmpeg 并将其添加到系统的 PATH 环境变量中。 如果您已经安装了 FFmpeg 但仍然遇到此错误,可以尝试在代码中指定 FFmpeg 可执行文件的路径。例如,假设您已经将 FFmpeg 安装在 `C:\ffmpeg\bin` 目录下,您可以使用以下代码来指定 FFmpeg 的路径: ```python import os import cv2 import subprocess # 指定 FFmpeg 可执行文件的路径 ffmpeg_path = "C:/ffmpeg/bin/ffmpeg.exe" if not os.path.isfile(ffmpeg_path): raise FileNotFoundError("FFmpeg not found at: " + ffmpeg_path) # 打开 RTMP 推流进程 rtmp_url = "rtmp://your_rtmp_server_url" command = [ffmpeg_path, '-y', '-f', 'rawvideo', '-pix_fmt', 'bgr24', '-s', '640x480', '-i', '-', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-preset', 'ultrafast', '-f', 'flv', rtmp_url] proc = subprocess.Popen(command, stdin=subprocess.PIPE) # 打开摄像头并捕获实视频流 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if not ret: break # 将视频帧写入 RTMP 推流进程 proc.stdin.write(frame.tostring()) # 显示视频帧 cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # 关闭摄像头和 RTMP 推流进程 cap.release() cv2.destroyAllWindows() proc.stdin.close() proc.wait() ``` 在上面的代码中,我们添加了一个 `ffmpeg_path` 变量来指定 FFmpeg 可执行文件的路径。然后,我们检查该文件是否存在,并在启动 FFmpeg 进程使用该路径。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值