2

My problem is similar as redirect prints to log file

When I started coding with Python last year, I didn't know about logging till now. The problem is the code is getting bigger and bigger.

I've been trying solutions given, but none of them work properly as this code contains input(), print(), and os.system()

The closest solution was from shx2 in his answer to How to duplicate sys.stdout to a log file?

Let's say this is the code

import os, sys

class multifile(object):
    def __init__(self, files):
        self._files = files
    def __getattr__(self, attr, *args):
        return self._wrap(attr, *args)
    def _wrap(self, attr, *args):
        def g(*a, **kw):
            for f in self._files:
                res = getattr(f, attr, *args)(*a, **kw)
            return res
        return g

sys.stdout = multifile([ sys.stdout, open('log.txt', 'w') ])

name = input("\nWhat is your name: ")
print("Your name is",name)

age = input("\nHow old are you: ")
print("You're ",age)

print("\nYour IP Address is: ")
os.system('ipconfig | findstr IPv4')

Console Output

C:\>python script.py

What is your name: ABC
Your name is ABC

How old are you: 10
You're  10

Your IP Address is:
   IPv4 Address. . . . . . . . . . . : 127.0.0.1

C:\>

print output from the console was redirected to log.txt without any problem, but not the input and os.system

log.txt output

What is your name: Your name is ABC

How old are you: You're  10

Your IP Address is: 

Would it be possible to get everything (exactly the same) on the console to log.txt?

  • Possible duplicate of [Output on the console and file using python](https://stackoverflow.com/questions/11325019/output-on-the-console-and-file-using-python) – sahasrara62 Jan 04 '19 at 17:56
  • does that work with `input` ??? I don't think so –  Jan 04 '19 at 18:00
  • have you tried ? – sahasrara62 Jan 04 '19 at 18:07
  • 2
    @Sabrina: do I understand correctly that you want the log file to contain *both* a copy of the program's output and a copy of the program's input? That is, you want the log to contain both what the program printed and what the user typed in response? In that case you'll not only need to hook `sys.stdout` but `sys.stdin` as well. – Daniel Pryden Jan 04 '19 at 18:45

1 Answers1

0

One option is to redirect the output to a file

C:\>python script.py > filename.txt

I tried this and it seems to produce what you're looking for. Doing it this way though you won't the input prompts (e.g, 'What's your name' or 'How old are you') just a blank line with the cursor. See this image as an example.

  • My question: Would it be possible to get everything (exactly the same) on the console to log.txt? This solution will print out nothing on the console. I would like to get similar output on both console and log file –  Jan 05 '19 at 05:38
  • How about just opening a file and writing to it as you the program is executed. Something like this: `fileWriter = open('log.txt', 'w') name = input('What is your name?: ') fileWriter.write('What is your name?: ' + name + '\n') print('Your name is', name) age = input('How old are you?: ') fileWriter.write('How old are you?: ' + age + '\n') print('You\'re', age)` – BigCheeze45 Jan 09 '19 at 14:44