Why does Popen.communicate() return b'hi\n' instead of 'hi'?

后端 未结 4 889

Can someone explain why the result I want, \"hi\", is preceded with a letter \'b\' and followed with a newline?

I am using Python 3.3



        
4条回答
  •  庸人自扰
    2020-12-23 15:02

    The echo command by default returns a newline character

    Compare with this:

    print(subprocess.Popen("echo -n hi", \
        shell=True, stdout=subprocess.PIPE).communicate()[0])
    

    As for the b preceding the string it indicates that it is a byte sequence which is equivalent to a normal string in Python 2.6+

    http://docs.python.org/3/reference/lexical_analysis.html#literals

提交回复
热议问题