python Python中的错误处理实践

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python中的错误处理实践相关的知识,希望对你有一定的参考价值。

# Empty lists are created to store information to be logged. These lists are always appended with new information
error_messages = []
output_messages = []

# Timestamps are created (import time in necessary)
current_time = time.localtime() # Checking the local date and time of the computer
now1 = time.strftime('%Y%m%d %H:%M:%S', current_time) # Making a timestamp variable
tstamp = time.strftime('%Y%m%d_%H%M%S', current_time)

output_messages.append(now1 + " Fermentation reporting script has started")
output_messages.append(now1 + " Last 30 fermentations: %s" % ", ".join(FileKeys))
error_messages.append("  ERROR: The filtering couldn't be performed")

# A text file is opened and the messages are filled in and saved
with open(r"u:\Python\Fermentation reports\Logfile_" + tstamp +  ".txt", "w") as text_file:
    text_file.write("\n".join(output_messages))
    text_file.write("\n")
    text_file.write("\n".join(error_messages))

以上是关于python Python中的错误处理实践的主要内容,如果未能解决你的问题,请参考以下文章

Python中的异常处理

Golang 中更好的错误处理:理论和实践技巧

Python编程入门到实践

登录python的最佳实践

一文掌握 Python 异常处理的所有知识点

处理 PyMySql 异常 - 最佳实践