I need to log all the output values from my python programm onto a text file. Everytime i press run it should automatically write all the values on the terminal to the text file (not a new text file everytime rather the same file, I have already put a timestamp to differentiate).
I would really appreciate it if you guys can show me how it is done, i am very new to programming hence my knowledge in this field is rather weak. An example code block could be really useful for me. Thank you so much in advance.
print(100*"#")
try:
preflash = urllib.request.urlopen("http://10.10.10.2", timeout=3).getcode()
print("Web page status code:", preflash, "FAIL")
sys.exit(0)
except urllib.error.URLError:
correct = urllib.request.urlopen("http://192.168.100.5", timeout=10).getcode()
print("Web page status code:", correct)
print("IP address: 192.168.100.5 is reachable")
print(100*"#")
# Declare url String
url_str = 'http://192.168.100.2/globals.xml'
# open webpage and read values
xml_str = urllib.request.urlopen(url_str).read()
# Parses XML doc to String for Terminal output
xmldoc = minidom.parseString(xml_str)
# order_number from the xmldoc
order_number = xmldoc.getElementsByTagName('order_number')
ord_nmr = order_number[0].firstChild.nodeValue
# firmware_version from the xmldoc
firmware_version = xmldoc.getElementsByTagName('firmware_version')
frm_ver = firmware_version[0].firstChild.nodeValue
b = frm_ver.split()[0]
# hardware_version from the xmldoc
hardware_version = xmldoc.getElementsByTagName('hardware_version')
hrd_ver = hardware_version[0].firstChild.nodeValue
v = hrd_ver.split()[-1]
# mac_address from the xmldoc
mac_address = xmldoc.getElementsByTagName('mac_address')
mac_addr = mac_address[0].firstChild.nodeValue
# device_type from the xmldoc
device_type = xmldoc.getElementsByTagName('device_type')
dev_typ = device_type[0].firstChild.nodeValue
# Desired Values
d_ordernum = "58184"
d_hw_version = "1.00"
d_sf_version = "1.0.0"
pc_praefix = "7A2F7"
d_dev_typ = "TREE M-5TX PN IP67"
# Timestamp
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Eingabe von scancode
scancode_string = input()
scan_code_cropped_mac = scancode_string[12:]
scan_code_cropped_artikel = scancode_string[:5]
#print(scan_code_cropped_artikel)
#print(scan_code_cropped_mac)
print("Current device information: ")
print(now)
print(100*"")
# Order number
print("Desired Order number:",d_ordernum)
print("Order number from scancode :",scan_code_cropped_artikel)
print("Ordernumber from wbm: ", ord_nmr)
if d_ordernum == ord_nmr == scan_code_cropped_artikel:
print("Order number PASS")
else:
print("Order number does not match")
print(100*"")
# Hardware version
print("Desired Hardware Version:",d_hw_version)
print("Hardware Version from wbm: ", v)
if d_hw_version == v:
print("Hardware version PASS")
else:
print("Wrong Hardware Version")
print(100*"")
# Software version
print("Desired Software Version:",d_sf_version)
print("Software Version from wbm: ", b)
if d_sf_version == b:
print("Software version PASS")
else:
print("Wrong Software Version")
print(100*"")
# Mac address
print("Mac address from scancode :",scan_code_cropped_mac)
print("Mac address from wbm: ", mac_addr)
list_of_chars = mac_addr.split(":")
mac_address_string_joined = ''.join(list_of_chars)
if scan_code_cropped_mac == mac_address_string_joined:
print("Correct MAC address")
else:
print("Fail")
print(100*"")
d_product_code = pc_praefix + "-" + d_sf_version + "-" + d_hw_version
product_code = pc_praefix + "-" + b + "-" + v
print("Desired product code: ",d_product_code )
print("Product code of current device: ", product_code )
print(100*"")
print("Desired device type:",d_dev_typ)
print("Device type from wbm: ", dev_typ)
if d_dev_typ == dev_typ:
print("Device type PASS")
else:
print("Wrong device type")
print(100*"")
Here is the terminal output. I must log all this information onto one file everytime I run the programm.
####################################################################################################
Web page status code: 200
IP address: 192.168.100.5 is reachable
####################################################################################################
58184#99AF0M000F9EF41A80
Current device information:
2021-04-14 19:26:04
Desired Order number: 58184
Order number from scancode : 58184
Ordernumber from wbm: 58184
Order number PASS
Desired Hardware Version: 1.00
Hardware Version from wbm: 1.00
Hardware version PASS
Desired Software Version: 1.0.0
Software Version from wbm: 1.0.0
Software version PASS
Mac address from scancode : 000F9EF41A80
Mac address from wbm: 00:0F:9E:F4:1A:80
correct MAC address
Desired product code: 7A2F7-1.0.0-1.00
Product code of current device: 7A2F7-1.0.0-1.00
Desired device type: TREE M-5TX PN IP67
Device type from wbm: TREE M-5TX PN IP67
Device type PASS