尝试将代码从 Python2.7 转换为 Python 3.5



>我正在尝试编写一段代码来捕获机器的服务标签。 下面是代码:

import os,sys,time
import os.path
import subprocess
import string
test_cmd='wmic bios get serialnumber /value'
output = subprocess.check_output(test_cmd)
start=output.rfind("SerialNumber=")+13
output=output[start:]
servicetag=output
print ("Service Tag = %s" %servicetag) #get the service tag

该代码似乎可以在 Python2.7 中工作,但我无法让它在 Python3.5 中工作。 错误消息如下所示,我无法从中做出任何反应。

start=output.rfind("SerialNumber=")+13
TypeError: argument should be integer or bytes-like object, not 'str'"

有谁知道出了什么问题?

output = subprocess.check_output(test_cmd)

对于 Python 2,output将是str型,对于 Python 3,它将是bytes

。最简单的解决方案是将输出从check_output转换为bytesstr

最新更新