I want to store the commands and their output to be stored in log.txt . So, i am assigning customer Logger class object to sys.stdout with write method overriden to store the output both in file as well as to print in console. The problem here is that when sys.stdout is overriden, ‘tab’ autocompletion of commands in ABC class ( hi,hello,shutdown ) is not working and up and down arrows are also not working for forward and backward history commands.
I expect that when ‘hel’ is typed in command above and then if tab is pressed, it should display ‘hello help’ in the next line. Pressing tab shouldn’t add 4 spaces to the command being typed. Pleas help with working solution in which auto completion of commands with tab,up/down arrow works and the commands and their output should store in a file ‘log.txt’. Please mind that i am running this program in red had linux 7 OS, not windows.
Thanks for your reply. But, output of commands is not getting written to file.
In above solution, we are passing Logger object as stdout to the cmd.py file. So, the content in cmd.py file( eg. output of help command, doc_string of any method) will only be written to log.txt. content in do_* methods ABC class is not written to log.txt. We want sys.stdout to be overriden with Logger object in current python file. Then only the content in do_* methods will be written to the log.txt file.
Tried the following code for tab auto completion using readline module. Still it’s not working.
The challenge here is how do we know ‘message’ is from sys.stdout or sys.stderr ?
If we use above class, everything will be printed twice in console. Is there any condition we can try
so that the write method will be similar to following.
if condition :
Our intension here is to print message based on which method it was called from, either sys.stdout or sys.stderr.