1

I am trying to log whatever is printed in console to a file(It should print in both console and in a file).i am using a command, that is logging the output to a file, but i am facing two problems: 1. Not printing the output on console, printing directly to a file. 2. If i am using sleep command it did not work.Can anyone help me in this with python codes. Here is my code

import  time    
sys.stdout = open("for_posterity.txt", "a")
def main():
    while True:
       fun1()
       fun2()
def fun1():
    time.sleep(1)
    print("inside fun 1")
def fun2():
    time.sleep(1)
    print("inside fun 2")
if __name__ == '__main__':
    main()
Ayush
  • 37
  • 6
  • exactly! i want the code to log. – Ayush Sep 22 '18 at 18:11
  • See [this](https://stackoverflow.com/questions/6674327/redirect-all-output-to-file) – Lucas Sep 22 '18 at 18:14
  • This is the program that will run infinite times..whatever output is printed on python shell should be copied to a file. i want a code for that. – Ayush Sep 22 '18 at 18:15
  • @ Lucas Can you please help me in windows. i think the link you shared is for LINUX. – Ayush Sep 22 '18 at 18:16
  • Kindly check the answer [here](https://stackoverflow.com/questions/4828885/how-to-direct-output-into-a-txt-file-in-python-in-windows) . it's clear and it shall help you. – Hadeel A. Sep 22 '18 at 18:29
  • @ Hadeel A i am using a command, that is logging the output to a file, but i am facing two problems: 1. Not printing the output on console, printing directly to a file. 2. If i am using sleep command it did not work. Here is the code i am using: sys.stdout = open("for_posterity.txt", "a") – Ayush Sep 22 '18 at 18:46
  • You could also do it external to the script with the `tee` command. – Keith Sep 23 '18 at 06:26

2 Answers2

2

When you changed sys.stdout, you lost the ability to write to the console. This code does exactly what you told it to do. If I look at for_posterity.txt, it says

inside fun 1
inside fun 2
inside fun 1
inside fun 2
...

If you want a logging function which does both, you have to actually do both.

def output(message):
    with open("for_posterity.txt", "a") as logfile:
        print (message)
        logfile.write(message + "\n")

def fun1():
    time.sleep(1)
    output("inside fun 1")

def fun2():
    time.sleep(1)
    output("inside fun 2")

However, when you get more advanced, you will want to use the logging module, which you can configure to write to as many or as few places as you want, and it can decide based on what's happening.

https://docs.python.org/3/library/logging.html

Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
  • @Ayush Please accept the answer if it worked for you. – Keith Sep 23 '18 at 06:24
  • @Kenny Can't i use print statement instead of output statement, as because output statement doesn't have multiple parameters. – Ayush Sep 24 '18 at 06:32
  • It could take multiple parameters, just like print, but that is a whole new topic. If you just want to intercept stdout, independently of the program, that is an operating system question -- it has nothing to do with what happens in the program. – Kenny Ostrom Sep 24 '18 at 17:45
0

Update - Added file.close() as without that it doesn't work in python 3

def print_and_write(content):
    print(content)
    file = open("for_posterity.txt", "a")
    file.write(content)
    file.close()

def main():
    while True:
       print_and_write(fun1())
       print_and_write(fun2())
def fun1():
    return("inside fun 1")
def fun2():
    return("inside fun 2")
if __name__ == '__main__':
    main()
Anoop Nair
  • 171
  • 1
  • 2
  • 13
  • Sorry but i didn't worked for me. Problem is, output is not copied to the file. – Ayush Sep 22 '18 at 19:21
  • Still Thanks a lot for your precious time. :) – Ayush Sep 22 '18 at 19:22
  • I think you are running Python 3. You need to use print() instead of print in that case. It should work. I have updated the code. Please post the error if this doesn't work. – Anoop Nair Sep 23 '18 at 04:25
  • yes i am using python 3.7, i did the same thing in print statement still not working. i am only getting output in stdout. – Ayush Sep 23 '18 at 05:47
  • @ayush Got it.. I usually work on python 2.7. With python 3, seems like we also need to explicitly close the file once the data is written. So, I have updated the code to reflect the same and tested in python 3.7. It is working now. You can check as well. – Anoop Nair Sep 23 '18 at 09:13
  • !! It worked but can't i write print statement in place of return ? As because i am not using any return statement. – Ayush Sep 24 '18 at 05:57