zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Python3 subprocess.check_output返回值转string

2023-03-15 23:24:02 时间

Python3中的subprocess.check_output函数可以执行一条sh命令,并返回命令的输出内容,用法如下:

output = subprocess.check_output(["python3", "xx.py"], shell = False)

该函数两个参数第一个表示命令内容,因为中间有空格所以用中括号这种形式,同时制定shell=False表示命令分开写了。而该命令执行后的输出内容会返回给output变量。

需要注意的是这个output变量并不是一个string,也就是说不能用string的一些函数,比如你想知道返回的输出中是否包含某个字符串:

output = subprocess.check_output(["python3", "xx.py"], shell = False)
if (output.find("yes") >= 0): print("yes")
else: print("no")

这样执行后不会有任何输出,因为find()函数是给string用的,而这里的output其实不是一个string,那它是个什么呢?

我们看看python3的subprocess.check_output的文档

By default, this function will return the data as encoded bytes. The actual encoding of the output data may depend on the command being invoked, so the decoding to text will often need to be handled at the application level.

也就是说,返回的其实是一个编码后的比特值,实际的编码格式取决于调用的命令,因此python3将解码过程交给应用层,也就是我们使用的人来做。

这样就清晰了,要对输出使用stirng的操作,需要先通过解码将其转换成string:

output = subprocess.check_output(["python3", "xx.py"], shell = False)
out = output.decode()
if (out.find("yes") >= 0): print("yes")
else: print("no")

这样就可以正常判断了。

查看作者首页