Vamei前辈出的练习题,个人解答,留存记忆。
http://www.cnblogs.com/vamei/archive/2012/08/31/2661870.html
import os,re import datetime filename = "output_1981.10.21.txt" print "Old file name is: " + filename gettime = re.search("(?P<year>\d{4}).(?P<month>\d{2}).(?P<day>\d{2})",filename) year = gettime.group("year") month = gettime.group("month") day = gettime.group("day") date = year + "-" + month + "-" + day print "Get the date is: " + date transform = datetime.date(int(year),int(month),int(day)) weekday = transform.weekday()+1 print date + " is week: " + str(weekday) fix_name = "output_" + year + "-" + month + "-" + day + "-" + str(weekday) + ".txt" if os.path.exists(filename): os.rename(filename,fix_name) print "rename complete" print "New file name is: " + fix_name else: print "This file is not exist here: " + filename
re.sub 还需要加强理解和运用。