Avatar ·

Redirecting terminal output into a variable

📁 python, вопрос, пример

Help to understand the redirect CMD output to the variable, because The variable is assigned only the Exit_code of the process. For clarity, there are a couple of examples: an example

   impress  sys, os command =  "" Tasklist ""  result1 = os.system (commander)   print  (result1) # at the exit 0      

Example 2

CommAND = "" Tasklist & gt; TEMP.TXT "" result2 = os.system (commander) print (result2) # at the output of the output. 0 with open ( 'r' ) as f: result2 = f.read () print (result2) # will derive the result of the execution of TASKLIST

the question: the question: the question: the question: the question: the question: the question: the question Is it possible to redirect the Windows terminal output without using the TEMP.TXT file, tobish immediately unload the value to the variable.? SYS.STDOUT tried.

  sys.stout =  open  ( 'TEMP.TXT' ,  'w' ) #write 0 in the file  os.system ( '' 'Tasklist' ) #The result of the execution itself will be displayed in the Intertator  file =  open  ( '' 'Temp.txt' ,  '`` ). Read ()   print  (file) # will display 0