Home > OS >  I am getting some data using powershell command from python without having a cmd in windows! how can
I am getting some data using powershell command from python without having a cmd in windows! how can

Time:10-28

I am getting some data using PowerShell command from python without having a cmd in windows by this command!

import os
windows_info = os.system('powershell.exe get-physicaldisk')

and it's successfully working. but this output gives an 'int' output! I tried to covert this into a string by using,

windows_infoEG = str(windows_info)

but when I tried to print this it won't print anything(but when I check its type its says 'str'). how can I turn this into a string in order to write the output into a text file!

this is my whole code.

 import os
    
    windows_info = os.system('powershell.exe get-physicaldisk')
    print(type(windows_info)) 
    
    windows_infoEG = str(windows_info)
    print(type(windows_infoEG)) 
    
    print(windows_infoEG)
    
    f = open("hardDriveInfo.txt", "w")
    f.write("Hard Drive Info"   windows_infoEG)
    f.close()

thank you!

CodePudding user response:

os.system returns you the "exit code" of the process (usually 0 for success, non-zero for failure). That's why you get an integer. It does NOT return to you the standard output of the command. For that, you need the subprocess module.

windows_info = subprocess.check_output(['powershell.exe','get-physicaldisk']).decode('utf-8')
  • Related