python 匿名函数赋给变量只能用一次

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 3838

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'

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1