Python subprocess.Popen()没有显示正确的stdout subprocess.call()

时间:2017-04-26 11:57:40

标签: python windows subprocess

当我在Windows 7中执行以下脚本时,它会针对这两种情况显示不同的stdout格式。第一个stdout显示内联字符串,第二个显示与在Windows cmd.exe中执行命令相同。我的代码出了什么问题?

import subprocess

process = subprocess.Popen(['dir'], stdout=subprocess.PIPE, shell=True)
stdout = process.communicate()[0]
print('STDOUT:{}'.format(stdout)) #first stdout

subprocess.call('dir', shell=True) #second stdout

first stdout

C:\ProgramData\Anaconda3\python.exe C:/Users/user/PycharmProject/spython_to_cmd_communication.py
STDOUT:b' C \xb5\xe5\xb6\xf3\xc0\xcc\xba\xea\xc0\xc7 \xba\xbc\xb7\xfd: Windows7\r\n \xba\xbc\xb7\xfd \xc0\xcf\xb7\xc3 \xb9\xf8\xc8\xa3: 8623-6624\r\n\r\n C:\\Users\\user\\PycharmProjects\xb5\xf0\xb7\xba\xc5\xcd\xb8\xae\r\n\r\n2017-04-26  \xbf\xc0\xc8\xc4 08:44    <DIR>          .\r\n2017-04-26  \xbf\xc0\xc8\xc4 08:44    <DIR>          ..\r\n2017-04-26  \xbf\xc0\xc8\xc4 08:44    <DIR>          .idea\r\n2017-04-20  \xbf\xc0\xc8\xc4 08:03             2,652  \xbf\xc0\xc8\xc4 08:29             4,664 listing_stock_code_business.py\r\n2017-04-26  \xbf\xc0\xc8\xc4 08:44               241 python_to_cmd_communication.py\r\n2017-04-20  \xbf\xc0\xc8\xc4 08:24             1,780   \xbf\xc0\xc8\xc4 08:24    <DIR>          __pycache__\r\n               4\xb0\xb3 \xc6\xc4\xc0\xcf               9,337 \xb9\xd9\xc0\xcc\xc6\xae\r\n               4\xb0\xb3 \xb5\xf0\xb7\xba\xc5\xcd\xb8\xae  38,024,884,224 \xb9\xd9\xc0\xcc\xc6\xae \xb3\xb2\xc0\xbd\r\n'
 C ����̺��� ����: Windows7
 ���� �Ϸ� ��ȣ: 8

第二个标准

C:\Users\user\PycharmProjects\python_to_cmd_communication���͸�

2017-04-26  ���� 08:44    <DIR>          .
2017-04-26  ���� 08:44    <DIR>          ..
2017-04-26  ���� 08:44    <DIR>          .idea
2017-04-26  ���� 08:44               241 python_to_cmd_communication.py
2017-04-20  ���� 08:24    <DIR>          __pycache__
               4�� ����               9,337 ����Ʈ
               4�� ���͸�  38,024,884,224 ����Ʈ ����

Process finished with exit code 0

1 个答案:

答案 0 :(得分:0)

这类似于具有相同内容的exe文件被调用的事实。您有时可以安装python pip install x或conda pip install x或vs python pip install x。