Exit interactive prompt inside script and move to next line

106 Views Asked by At

Hey Im trying to print output of an interactive command to a file inside a python script and move on to next line.

I am not sure how to achieve this. I have tried:

os.system("mnamer foo.mkv > mnamer.txt")

FYI mnamer can be imported and called from inside the script with "mnamer"

the above command logs the info I need in a file but I need it to move past the prompt and read the next line of code.

Is there a python specific way of doing this?

1

There are 1 best solutions below

0
On BEST ANSWER

If you can import mnamer as a python module, do that, use it this way, and log its outputs to a file by temporarily assigning sys.stdout and sys.stderr to a file:

import mnamer
import sys

logfile = open("/path/to/log/file.txt", "w") # open the logfile
stdout, stderr = sys.stdout, sys.stderr # make copies of these to be able to restore them after the mnamer commands
sys.stdout = logfile # assign stdout to logfile, infos will be written there
sys.stderr = logfile # assign stderr to logfile, errors will be written there too
# put your mnamer commands here
mnamer.some_method_of_mnamer_module()
sys.stdout = stdout # restore stdout so that further infos will be printed to terminal again
sys.stderr = stderr # restore stderr so that further errors will be printed in terminal again
logfile.close() # close the logfile