在python3中使用subprocess的check_out方法时,因为该输出为byte类型,所以如果要查看具体的内容时需要进行转码,如果转码不对话,会影响内容输出的可读性,如下:

#1,输出解码不带参数

 # -*- coding:utf-8 -*-

 import subprocess
cmd = r"ping www.baidu.com"
result = subprocess.check_output(cmd)
print(result.decode()) # decode中不带参数,默认是以utf-8解码 输出报错:
Traceback (most recent call last):
File "E:/debug.py", line 12, in <module>
print(result.decode()) # decode中不带参数
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 2: invalid continuation byte Process finished with exit code 1

#2,输出解码带上 unicode_escape 参数,会显示乱码

 # -*- coding:utf-8 -*-

 import subprocess
cmd = r"ping www.baidu.com"
result = subprocess.check_output(cmd)
print(result.decode("unicode_escape")) # decode中带参数 unicode_escape 输出显示乱码如下:
ÕýÔÚ Ping www.a.shifen.com [14.215.177.39] ¾ßÓÐ 32 ×Ö½ÚµÄÊý¾Ý:
À´×Ô 14.215.177.39 µÄ»Ø¸´: ×Ö½Ú=32 ʱ¼ä=6ms TTL=47
À´×Ô 14.215.177.39 µÄ»Ø¸´: ×Ö½Ú=32 ʱ¼ä=6ms TTL=47
À´×Ô 14.215.177.39 µÄ»Ø¸´: ×Ö½Ú=32 ʱ¼ä=6ms TTL=47
À´×Ô 14.215.177.39 µÄ»Ø¸´: ×Ö½Ú=32 ʱ¼ä=6ms TTL=47 14.215.177.39 µÄ Ping ͳ¼ÆÐÅÏ¢:
Êý¾Ý°ü: ÒÑ·¢ËÍ = 4£¬ÒѽÓÊÕ = 4£¬¶ªÊ§ = 0 (0% ¶ªÊ§)£¬
Íù·µÐг̵ĹÀ¼ÆÊ±¼ä(ÒÔºÁÃëΪµ¥Î»):
×î¶Ì = 6ms£¬× = 6ms£¬Æ½¾ù = 6ms Process finished with exit code 0

#3、输出解码带上 gbk 参数,显示正常

 # -*- coding:utf-8 -*-

 import subprocess
cmd = r"ping www.baidu.com"
result = subprocess.check_output(cmd)
print(result.decode("gbk")) # decode中带参数 gbk 输出显示正常如下:
正在 Ping www.a.shifen.com [14.215.177.39] 具有 32 字节的数据:
来自 14.215.177.39 的回复: 字节=32 时间=7ms TTL=47
来自 14.215.177.39 的回复: 字节=32 时间=6ms TTL=47
来自 14.215.177.39 的回复: 字节=32 时间=7ms TTL=47
来自 14.215.177.39 的回复: 字节=32 时间=7ms TTL=47 14.215.177.39 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 6ms,最长 = 7ms,平均 = 6ms Process finished with exit code 0

subprocess之check_out用法的更多相关文章

  1. 【Android自动化】Subprocess.check_output()简单用法

    # -*- coding:utf-8 -*- import os import sys import subprocess from uiautomator import device as d cm ...

  2. subprocess 的 Popen用法

    使用Popen方法时,需要获取输出内容时可以按如下方法获取: # -*- coding:utf-8 -*- import subprocess cmd = r"ping www.baidu. ...

  3. 模块sys, os, glob, pickle, subprocess常见用法

    参考python常用标准库 http://blog.51cto.com/lizhenliang/1872538 一. sys   1. sys.argv 脚本名1.py, 命令行中执行python 1 ...

  4. Python多进程(1)——subprocess与Popen()

    Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...

  5. Bigger-Mai 养成计划,subprocess模块

    subprocess模块是python从2.4版本开始引入的模块.主要用来取代 一些旧的模块方法,如os.system.os.spawn*.os.popen*.commands.*等.subproce ...

  6. python之路--subprocess,粘包现象与解决办法,缓冲区

    一. subprocess 的简单用法 import subprocess sub_obj = subprocess.Popen( 'dir', #系统指令 shell=True, #固定方法 std ...

  7. Python模块subprocess

    subprocess的常用用法 """ Description: Author:Nod Date: Record: #-------------------------- ...

  8. python中subprocess模块

    subprocess  模块 subprocess称之为子进程,进程是一个正在进行的程序 子进程是由另一个正在运行的程序启动的程序,例如QQ聊天点击一个链接,打开了浏览器,那么浏览器称之为QQ的子进程 ...

  9. python常用模块-调用系统命令模块(subprocess)

    python常用模块-调用系统命令模块(subprocess) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. subproces基本上就是为了取代os.system和os.spaw ...

随机推荐

  1. B.Obtain Two Zeroes

    题目:包含两个零 题意:你被给予两个数a和b,你可以对这两个数进行操作 每次操作你可以选择任意的正整数x,可以进行a = a - x,b = b - 2x或者a = a - 2x,b = b - x两 ...

  2. Java instanceof 和 Class.isInstance()区别与应用

    一.instanceof 关键字 instanceof 关键字用于判断某个实例是否是某个类的实例化对象,形如: String.class instanceof Class "test&quo ...

  3. Android中实现异步轮询上传文件

    前言 前段时间要求项目中需要实现一个刷卡考勤的功能,因为涉及到上传图片文件,为加快考勤的速度,封装了一个异步轮询上传文件的帮助类 效果  先上效果图 设计思路 数据库使用的框架是GreenDao,一个 ...

  4. pyplot概述

            matplotlib.pyplot 是命令行风格的函数集,让matplotlib看起来像MATLAB.Each一样工作.pyplot函数能够对画布(figure)进行一些改变,例如:创 ...

  5. 解决:Sass Loader has been initialised using an options object that does not ma tch the API schema.

    今天是犯傻的一天,第一回用sass遇到了bug: 结果就是:<style lang = 'scss'>.写成了<style lang = 'sass'> (脑子要清醒一点.太笨 ...

  6. 【NodeJS】nvm

    [NodeJS]nvm node多版本管理 NVM_HOME=C:\env\nvm NVM_SYMLINK=C:\env\nodejs 查看版本 nvm v 查看当前使用的node版本 nvm cur ...

  7. 【Spring Boot】定时任务

    [Spring Boot]定时任务 测试用业务Service package com.example.schedule.service; import org.springframework.ster ...

  8. 还不懂MySQL索引?这1次彻底搞懂B+树和B-树

    前言 看了很多关于索引的博客,讲的大同小异.但是始终没有让我明白关于索引的一些概念,如B-Tree索引,Hash索引,唯一索引….或许有很多人和我一样,没搞清楚概念就开始研究B-Tree,B+Tree ...

  9. 还不知道如何实践微服务的Java程序员,这遍文章千万不要错过!

    作者:古霜卡比 前言 本文将介绍微服务架构和相关的组件,介绍他们是什么以及为什么要使用微服务架构和这些组件.本文侧重于简明地表达微服务架构的全局图景,因此不会涉及具体如何使用组件等细节. 要理解微服务 ...

  10. python学习-pandas

    import pandas as pd # DataForm 二维数据# print(pd.read_excel("datas.xlsx")) # 多行数据 - 加载表单s = p ...