如何将所有终端输出记录/协议到python中的文本文件中[重复]

Posted

技术标签:

【中文标题】如何将所有终端输出记录/协议到python中的文本文件中[重复]【英文标题】:how to Log/protocoll all of the terminal output onto a text file in python [duplicate] 【发布时间】:2021-07-09 19:00:57 【问题描述】:

我需要将我的 python 程序的所有输出值记录到一个文本文件中。每次我按运行它应该自动将终端上的所有值写入文本文件(不是每次都是新的文本文件,而是同一个文件,我已经放了一个时间戳来区分)。

如果你们能告诉我它是如何完成的,我将非常感激,我对编程很陌生,因此我在这个领域的知识相当薄弱。 一个示例代码块可能对我非常有用。 非常感谢您。

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*"")

这是终端输出。每次运行程序时,我都必须将所有这些信息记录到一个文件中。

####################################################################################################
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

【问题讨论】:

https://docs.python.org/3/howto/logging.html 【参考方案1】:

使用记录器

import logging

logging.basicConfig(
    filename="my-logs.log",
    filemode="a",
    level=logging.INFO,
)
logger = logging.getLogger("log")

# Software version
logger.info("Desired Software Version:",d_sf_version)
logger.info("Software Version from wbm: ", b)
if d_sf_version == b:
    logger.info("Software version PASS")
else:
    logger.info("Wrong Software Version")
logger.info(100*"")

# Mac address
logger.info("Mac address from scancode :",scan_code_cropped_mac)
logger.info("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:
    logger.info("Correct MAC address")
else:
    logger.info("Fail")
logger.info(100*"")

d_product_code = pc_praefix + "-" + d_sf_version + "-" + d_hw_version
product_code = pc_praefix + "-" + b + "-" + v
logger.info("Desired product code: ",d_product_code )
logger.info("Product code of current device: ", product_code )
logger.info(100*"")

logger.info("Desired device type:",d_dev_typ)
logger.info("Device type from wbm: ", dev_typ)
if d_dev_typ == dev_typ:
    logger.info("Device type PASS")
else:
    logger.info("Wrong device type")
logger.info(100*"")

【讨论】:

以上是关于如何将所有终端输出记录/协议到python中的文本文件中[重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何在文本文件中将输出复制到 bs4 中的终端

如何将标准输出和标准错误重定向到 Python 中的记录器

如何将标准输出和标准错误重定向到 Python 中的记录器

我如何使用python将终端输出到网格中的tkinter框架?

ubuntu 把终端信息输出到文本文件中的方法

如何将彩色文本输出到 Linux 终端?