【发布时间】: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