import os,subprocess
command=r'pyinstaller C:\Users\user\Desktop\Питон\hello_world.py'
output=os.popen(command)
print(output.read())
我想获取命令行的输出,但该方法只返回换行符('\n'):
>>>
如果您在命令行手动键入此命令,它将输出:
94 INFO: PyInstaller: 3.6
94 INFO: Python: 3.8.1
94 INFO: Platform: Windows-10-10.0.14393-SP0
98 INFO: wrote C:\Users\user\hello_world.spec
99 INFO: UPX is not available.
102 INFO: Extending PYTHONPATH with paths
['C:\\Users\\user\\Desktop\\Питон', 'C:\\Users\\user']
102 INFO: checking Analysis
102 INFO: Building Analysis because Analysis-00.toc is non existent
...
13823 INFO: Building COLLECT COLLECT-00.toc completed successfully.
这是我需要的输出我尝试了子进程模块的选项,结果是一样的:
output=subprocess.Popen(command,stdout=subprocess.PIPE)
print(output.communicate()[0].decode('utf-8'))
>>>
这篇文章不是来的
stdout
,而是来的stderr
。getoutput
将它们合并为一个,因此可以正常工作。但
getoutput
最好不要使用它,因为它会忽略返回码。还有.
check_output
_getstatusoutput
使用 getoutput 方法解决了该问题:
该方法直接返回命令行的输出。