【问题标题】:Get the output of .exe file in Python3 on Windows在 Windows 上获取 Python3 中 .exe 文件的输出
【发布时间】:2023-03-17 18:08:01
【问题描述】:

我一直在尝试获取以下命令“manage-bde -status”的输出,该命令在我的 Windows cmd 命令提示符下运行良好,但通过使用 python 程序。相同的命令不适用于 python 子进程,所以我不得不启动 manage-bde.exe 文件。

所以我的代码现在看起来像这样:

import os, platform, subprocess

################This part is only helpful for resolving 32 bit/64 bits issues##########
system_root = os.environ.get('SystemRoot', 'C:\\Windows');
if (platform.architecture()[0] == '32bit' and platform.machine() == 'AMD64'):
    system32='Sysnative'
else:
    system32='System32'

manage_bde = os.path.join(system_root, system32, 'manage-bde.exe')
print(manage_bde)
#######################################################################################

stdout=subprocess.check_output(['start',manage_bde,'-status'])
print('Output:'+stdout)

我使用 Python 3.7.0 从 cmd 命令行启动它。问题是我得到以下输出:

    C:\WINDOWS\Sysnative\manage-bde.exe (this is from my print(manage_bde))
    Traceback (most recent call last):
    File "testCLI.py", line 13, in <module>
    stdout=subprocess.check_output(['start',manage_bde,'-status'])
    File "d:\Profiles\user\AppData\Local\Programs\Python\Python3\lib\subprocess.py", line 376, in check_output
    **kwargs).stdout
  File "d:\Profiles\user\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 453, in run
    with Popen(*popenargs, **kwargs) as process:
  File "d:\Profiles\user\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 756, in __init__
    restore_signals, start_new_session)
  File "d:\Profiles\user\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1155, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] Specified file was not found

我从 D: Drive 启动它。有谁知道我错过了什么?

【问题讨论】:

  • 应该只是subprocess.check_output([manage_bde, '-status'])。没有“start.exe”。 start 是 CMD shell 中的内置命令。如果您希望它在新的控制台中运行,请添加参数creationflags=subprocess.CREATE_NEW_CONSOLE

标签: python windows python-3.x cmd bitlocker


【解决方案1】:

您可能需要以管理员权限运行脚本。

如果文件存在于文件系统中并且你得到FileNotFoundError: [WinError 2],你可以测试脚本是否有足够的权限来查看文件:

os.stat(manage_bde)

【讨论】:

    【解决方案2】:

    是的,我有足够的权限查看该文件:os.stat(manage_bde) 正在工作。 我已经改变了我的参数,比如 eryksun 建议的:

    stdout = subprocess.getoutput([manage_bde, '-status'])
    

    现在我可以启动程序(但只能使用具有管理员权限的 shell)并获取输出,谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 2010-10-19
      相关资源
      最近更新 更多