[Python3]subprocess.check_output () The output of the Python3 is bytes instead of a string, and a decoding process is added during the actual use Decode (), otherwise there will be a problem

Source: Internet
Author: User

Output an error according to the customary code of the past Python2

1 #-*-coding:utf-8-*-2 " "3 Created on July 21, 20184 5 @author: Lenovo6 " "7 ImportOS8 ImportSYS9 ImportsubprocessTen  fromUiautomatorImportDevice as D Onecmd = r'adb install f:\ listening. APK' Ainfo = subprocess.check_output (cmd). Split ("\ r \ n") - Print(info)

Output as follows, error

1 Traceback (most recent): 2   " C:\Users\lenovo\eclipse-workspace\WOO\src\debug\debug1.py "  in <module>3     info = subprocess.check_output (cmd). Split ("\ r \ n " )4is not'str'

The query Python3 document has the following description:

By default, this function would return the data as encoded bytes. The actual encoding of the output data may depend in the command being invoked, so the decoding to text would often need to Be handled at the application level. That is, the return is actually an encoded bit value, the actual encoding format depends on the command called, so python3 the decoding process to the application layer, that is, we use the person to do.
Verify that the process is not so, we print out the type of the Subprocess.check_output () as follows, indeed for the bytes type, need to be artificially converted once to string

1 #-*-coding:utf-8-*-2 " "3 Created on July 21, 20184 5 @author: Lenovo6 " "7 ImportOS8 ImportSYS9 ImportsubprocessTen  fromUiautomatorImportDevice as D Onecmd = r'adb install f:\ listening. APK' Ainfo =subprocess.check_output (cmd) - Print(Type (info))

The output is as follows:

<class'bytes'>

The correct is as follows:

1 #-*-coding:utf-8-*-2 " "3 Created on July 21, 20184 5 @author: Lenovo6 " "7 ImportOS8 ImportSYS9 ImportsubprocessTen  fromUiautomatorImportDevice as D Onecmd = r'adb install f:\ listening. APK' Ainfo =subprocess.check_output (cmd) -Info1 =Info.decode () - Print(Info1.split ('\ r \ n'))

The output is as follows:

[' Success ', ']

[Python3]subprocess.check_output () The output of the Python3 is bytes instead of a string, and a decoding process is added during the actual use Decode (), otherwise there will be a problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.