Get the output of .exe file in Python3 on Windows

659 Views Asked by At

I've been trying to get the output of the following command 'manage-bde -status' which works fine on my windows cmd commande prompt, but by using a python program.The same command doesn't work on python subprocess, so I had to launch the manage-bde.exe file.

So my code looks like this now :

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)

I'm launching it from cmd commande line, with Python 3.7.0. The problem is that I get the following output :

    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

I launch it from the D: Drive. Does anyone know what I missed ?

2

There are 2 best solutions below

0
On BEST ANSWER

Yes I have enough permissions to see the file : the os.stat(manage_bde) is working. I have changed my parameters like eryksun proposed :

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

Now I can launch the program (but only with a shell with administrator rights) and get the output, thank you !

0
On

You probably need to run the script with administrator privileges.

If the file exists in the filesystem and you get FileNotFoundError: [WinError 2], you can test whether the the script has enough permissions to see the file with:

os.stat(manage_bde)