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

后端 未结 4 891

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 14:53

    b is the byte representation and \n is the result of echo output.

    Following will print only the result data

    import subprocess
    print(subprocess.Popen("echo hi", shell=True,stdout=subprocess.PIPE).communicate()[0].decode('utf-8').strip())
    

提交回复
热议问题