子进程 "类型错误:需要字节类对象,而不是'str'"

2024-10-02 18:21:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用a previously asked question a few years ago中的代码,但是,我相信这已经过时了。尝试运行代码时,收到上面的错误。我还是一个Python新手,所以我无法从类似的问题中得到太多的澄清。有人知道为什么会这样吗?

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]

print(getLength('bell.mp4'))

回溯

Traceback (most recent call last):
  File "B:\Program Files\ffmpeg\bin\test3.py", line 7, in <module>
    print(getLength('bell.mp4'))
  File "B:\Program Files\ffmpeg\bin\test3.py", line 6, in getLength
    return [x for x in result.stdout.readlines() if "Duration" in x]
  File "B:\Program Files\ffmpeg\bin\test3.py", line 6, in <listcomp>
    return [x for x in result.stdout.readlines() if "Duration" in x]
TypeError: a bytes-like object is required, not 'str'

Tags: inforreturnifbinstdoutfilesresult
2条回答

默认情况下,subprocess返回stdout或stderr流的bytes对象。这意味着您还需要在对这些对象的操作中使用bytes对象。"Duration" in x使用str对象。使用字节文字(注意b前缀):

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

或者,如果您知道使用的编码(通常是区域设置默认值,但您可以为子进程set ^{} or more specific locale environment variables),请先对数据进行解码:

return [x for x in result.stdout.read().decode(encoding).splitlines(True)
        if "Duration" in x]

另一种方法是告诉subprocess.Popen()通过将encoding参数设置为合适的编解码器将数据解码为Unicode字符串:

result = subprocess.Popen(
    ["ffprobe", filename],
    stdout=subprocess.PIPE, stderr = subprocess.STDOUT,
    encoding='utf8'
)

如果设置了text=True(Python 3.7及更高版本,在以前的版本中,此版本称为universal_newlines),则还可以使用system default codec启用解码,与用于open()调用的解码相同。在这种模式下,默认情况下管道是行缓冲的。

就像错误者说的,“持续时间”是一个字符串。然而,X是一个类似字节的对象,因为results.stdout.readlines()将输出中的行读取为字节码而不是字符串。

因此,将“Duration”存储在变量中,比如str_var,并使用str_var.encode('utf-8')将其编码为字节数组对象。

请参阅[此][1]。

[1]:Best way to convert string to bytes in Python 3?

相关问题 更多 >