I am having this problem where I can print out the powershell code output with the print() function, but when I try to do the same, except this time I write the output to a file, the only thing that is written in the file is "0", why would the printed output be different from when I write the same exact code, except that I this time "print" it to a text file.
I want the text file to contain exactly what the print function prints to the terminal, why isn't it working, and how can I get it to work?? Here are some pictures and the code:
import os
import time
def monitorprocess(process):
run = True
time_q = float(input("How many minutes before each check? "))
while run:
timespan = os.system(f'powershell New-TimeSpan -Start(Get-process {process}).StartTime')
try:
open(f'powershellpython\{process}.txt','x')
except:
pass
with open(f'powershellpython\{process}.txt',"w") as file:
file.write(str(timespan))
print(timespan)
time.sleep(time_q*60)
def processes():
process = input("What is the name of your process, if you are unsure, type 'get-process', and if you want to use ID (this works with multiple processes with the same name) type ID: \n")
if process == "get-process":
print(os.system("powershell get-process"))
process = input("What is the name of your process, if you are unsure, type 'get-process', and find your process: \n")
else:
monitorprocess(process)
processes()
And there is some more output with the print, that being "hours" and "days", but that does not really matter in this context.


python script.py > output.txt- and probably it should redirect console output to file (at least it works incmd.exeorbashon Linux). OR you should use other functions in modulesubprocess- likesubprocess.run()or subprocess.check_output() because withos.system()you can't catch output.