python 变量只用一次_葫芦的运维日志

下一篇

搜索

上一篇

浏览量 1500

2017/10/30 01:12

需求:

检查目录下文件包含时间格式 YYYY-mm-dd-HH 且文件后缀不为gz的文件,当文件数等于3时返回true其他情况false。

实现:

用匿名函数实现了此功能,却意外发现了 将匿名函数赋给变量后,重复执行只能得到第一次的结果。

匿名函数赋给变量,只能用一次。以后需要避免此坑。

python3.5:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>>

>>> chk = lambda b6=(datetime.datetime.now()+datetime.timedelta(hours=-6)).strftime('%Y-%m-%d-%H'), x = [s for s in filter(lambda li: not li.endswith('gz'),os.listdir())]:'%s success' % b6 if len([z for z in x if b6 in z])==3 else '%s failure' % b6

>>>

>>>

>>>

>>> os.listdir()

['video2018-10-29-18.log', 'two2018-10-29-19.log', 'music2018-10-29-18.log', '1.txt', 'one2018-10-29-19.log']

>>> chk()

'2018-10-29-19 failure'

>>> open('./three2018-10-29-19.log','w').close()

>>> chk()

'2018-10-29-19 failure'

>>> chk = lambda b6=(datetime.datetime.now()+datetime.timedelta(hours=-6)).strftime('%Y-%m-%d-%H'), x = [s for s in filter(lambda li: not li.endswith('gz'),os.listdir())]:'%s success' % b6 if len([z for z in x if b6 in z])==3 else '%s failure' % b6

>>> chk()

'2018-10-29-19 success'

上一篇

搜索

下一篇

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值