【问题标题】:How to receive cmd 'quser' output?如何接收 cmd 'quser' 输出?
【发布时间】:2019-11-28 12:26:27
【问题描述】:

我正在尝试构建 Python 代码,该代码将在 cmd 中运行 quser 程序并接收它的输出,然后运行注销到其中一个用户。

我尝试在 Python 3.7.3 中同时使用 ossubprocess 模块,但它似乎总是这样说:

'quser' 未被识别为内部或外部命令。

我还尝试了quser 可执行文件的完整路径。

import os
import subprocess

output = os.system("quser")
print(output)

# returns -- > 'quser' is not recognized as an internal or external command, operable program or batch file.

p = subprocess.Popen("quser", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print(out)

# returns -- > 1 b''

预期输出

USERNAME         SESSIONNAME        ID  STATE   IDLETIME  LOGON       TIME
John             console            1   Active  57        19/07/2019  12:27
Doe                                 2   Disc    57        19/07/2019  15:35

【问题讨论】:

  • 你不需要python-3.xpython-3.7标签(删除python-3.7);不过,你确实需要一个windows 标签。
  • 你试过check_output函数吗?
  • 如果我没记错的话 quser 只能在 System32 中工作 cmd.exe 所以如果你在 x64 机器上运行,默认的 cmd.exe 没有 quser。不知道为什么。
  • 它在 x64 机器上对我来说工作得很好。使用 subprocess 方法,我得到了你期望的输出。
  • 如果没有扩展名/完全限定路径(或使用 cmd.exe),您希望 "quser" 扩展到什么?将"quser" 替换为"C:\Windows\System32\quser.exe"

标签: python python-3.x windows subprocess


【解决方案1】:
subprocess.check_output(r"C:\Windows\Sysnative\quser.exe")

确实有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多