Encountered Python3 incompatible with Python2 's place

Source: Internet
Author: User

Execute the following code in Python3

>>>Importsubprocess>>> p=subprocess. Popen ('ls', shell=true,stdout=subprocess. PIPE)>>> d=P.stdout.read ()>>>DB'agent2.0.tgz\njdk1.8.0_152\njdk-8u152-linux-x64.tar.gz\nmha4mysql-manager-0.56-0.el6.noarch.rpm\ nmha4mysql-node-0.56-0.el6.noarch.rpm\nscript.rpm.sh\nscripts\n'>>> D.split ('\ n') Traceback (most recent): File"<stdin>", Line 1,inch<module>TypeError:'Str'Does notSupport the Buffer interface

Buffer interface allows an object to expose information about its underlying buffer, an example of using buffer interface is the write () method of the file object, and any object that exports a series of bytes through buffer interface can be written to the file. Python3 only supports bytes and Unicode encoding, and no longer supports STR

The workaround is to convert STR to tytes, and then back to the STR type after processing

>>> p=subprocess. Popen ('ls', shell=true,stdout=subprocess. PIPE)>>> d=P.stdout.read ()>>> d.split (Bytes ('\ n','UTF8')) [B'agent2.0.tgz'B'jdk1.8.0_152'B'jdk-8u152-linux-x64.tar.gz'B'mha4mysql-manager-0.56-0.el6.noarch.rpm'B'mha4mysql-node-0.56-0.el6.noarch.rpm'B'script.rpm.sh'B'Scripts'B"']

Encountered Python3 incompatible with Python2 's place

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.