# 本月文件和上个月文件
year = datetime.datetime.now().year
month = datetime.datetime.now().month
now_month_file = '{}.{:0>2}'.format(year, month)
last_month_file = '{}.{:0>2}'.format(year, month - 1)
if month == 1:
last_month_file = '{}.12'.format(year - 1)
这段代码非常巧妙的一点在于使用了格式化的方式保证输出的月份总是2位数{:0>2}代表的意思是该位置总是占两个字符的位置,且是右对齐,如果有空余的位置就用0来补齐。
另外一个亮点在于1月份的处理,单独处理1月份的上一个月。