我正在打开一个名为的文件,格式如下:
ex130626.log
exYYMMDD.log
下面的代码需要4位数的年份。如何得到像13这样的两位数年份?
today = datetime.date.today()
filename = 'ex{0}{1:02d}{2:02d}.log'.format(today.year, today.month, today.day)
最佳答案
取年的模数:
>>> import datetime
>>> today = datetime.date.today()
>>> filename = 'ex{:02}{:02}{:02}.log'.format(today.year%100, today.month, today.day)
>>> filename
'ex130625.log'
但更简单的方法是:
>>> today.strftime('ex%y%m%d.log')
'ex130625.log'
关于python - 以两位数年份命名的打开文件-Python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17311280/