Append text to text file with timestamp - Raspberry Pi Forums


i want append output python code text file. above python code , loop every 2 hour

code: select all

if response == 0:     print(hostname, "is up")      if option == 1:        print(option, "is option')        print('this option number 1')     elif option == 2:        print(option, "is option')        print('this option number 2')     else:        print(option, "is other option')        print('this result of other option') 
notice need following code append result text file.

code: select all

with open("test.txt", "a") myfile:     myfile.write("appended text") 
how can record every output text file , include time stamp it? example

code: select all

09:10  192.168.0.1 09:10  1 option 09:11   option number 1 11:15  192.168.0.1 11:10  1 option 11:11   option number 1 13:10  192.168.0.1 13:10  3 other option 13:11   result of other option 

this seems candidate built in python logging module rather spinning own version using print or writing file.

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

can modify format of log display time stamp before each entry.


raspberrypi



Comments