1

TypeError:类型 str 不支持缓冲区 API

在 python3.4.2 中运行此代码时

import subprocess

def getLength(filename):
  result = subprocess.Popen(["ffprobe", filename],
    stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
  return [x for x in result.stdout.readlines() if "Duration" in x]
4

1 回答 1

1

在 Python 3.x 中,读取子进程的标准输出会产生bytes而不是str.

使用bytes文字将解决您的问题(b在字符串文字之前添加)

return [x for x in result.stdout.readlines() if b"Duration" in x]

顺便说一句,readlines不需要。只需迭代result.stdout

return [x for x in result.stdout if b"Duration" in x]
于 2015-01-24T14:22:57.490 回答