【问题标题】:why is my code only printing the first line of cmd?为什么我的代码只打印 cmd 的第一行?
【发布时间】:2018-07-01 15:10:35
【问题描述】:

所以我正在学习如何从 python 中使用 cmd 并尝试这样做:

import subprocess
proc = subprocess.Popen('cmd.exe', stdin = subprocess.PIPE,  encoding = 
'utf8', stdout = subprocess.PIPE)
stdout, stderr = proc.communicate('dir c:\\')
print(stdout)

这显然不起作用,因为这是我收到的:

Microsoft Windows [版本 10.0.14393] (c) 2016 年微软公司。保留所有权利。

C:\Users\User\AppData\Local\Programs\Python\Python36-32>更多?

为什么只有一行而不是命令什么?

【问题讨论】:

    标签: python-3.x cmd


    【解决方案1】:

    this related question 的答案提出了一种在 Windows 上与 shell 进行通信的更好方法:

    import subprocess
    result = subprocess.check_output('cmd /k dir c:\\', shell = True)
    

    【讨论】:

      猜你喜欢
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 2019-11-17
      • 2022-01-13
      • 1970-01-01
      • 2015-12-17
      相关资源
      最近更新 更多