函数的好处

该博客展示了如何使用Python的logger函数来记录程序运行日志。通过定义一个logger函数,实现了在不同函数中调用并记录当前时间,将日志追加到文件a.txt中。同时,logger函数在test1、test2和test3三个测试函数中被调用,展示了其代码重用性和一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#总结函数的好处:代码重用,保持一致性,可扩展性
import time
def logger():
    time_format = "%Y-%m-%d %X"
    time_current = time.strftime(time_format)
    with open("a.txt","a+") as f:
        f.write("%s end action\n" %time_current)
def test1():
    print("in the test1")
    logger()
def test2():
    print("in the test2")
    logger()
def test3():
    print("in the test3")
    logger()
test1()  #运行函数
test2()
test3()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值