Python one-day question (1)

Source: Internet
Author: User

#有一个文件, the file name is Output_1981.10.21.txt. # use Python below: Read the datetime information in the file name and find out what day is the week. # renamed the file to output_yyyy-mm-dd-w.txt# (YYYY: Four-bit year, MM: two-bit month, DD: Two-bit day, W: one-digit week, and assumes Monday is the first day of the week) "'" thinking: 1, get file name, intercept date, generate date variable parameter ' ' Import os,reimport time,datetimeclass hello:def __init__ (self): print (' Ask for filename, take out date, figure out the day of the 1 year, change the filename ') def get_d        Ate (Self,filename): (filepath,tempfilename) = os.path.split (filename) #将文件名和路径分割开. (shotname,extension) = Os.path.splitext (tempfilename) #分离文件名与扩展名 datestr = Re.split (' _ ', shotname) [1] date = Re.split (' [.] ', DATESTR) return date #给个时间, find out which day of the 1 year is the "' Solution: 12 months a year to make a list, only February not confirmed,        Leap year 28 days, non-leap year 29 days, draw February days. The sum of the days of the preceding months and the number of days of the month, that is, when the first day of the Year "Def Which_day (self): num = 0 date_list = self.get_date (' D:\python\learn\        Output_1981.10.21.txt ') year =int (date_list[0]) month = Int (date_list[1]) day = Int (date_list[2]) sum = 0 #计算2月份的天数 if (year%4==0): num = ELif (year%400==0): num = else:num =29 month_list = [31, num,, 30, 31, 30, 31, 31 , +, +, +] for I in range (0,month-1): Sum +=month_list[i] #第几天 sum +=day ret Urn sum,year,month,day #获取当前系统时间 month-day week def Get_now_time (self): now = Time.localtime () weekday = TIME.S Trftime ('%w ', now] year = Datetime.datetime.now (). Year month = Datetime.datetime.now (). Month Day = Dat Etime.datetime.now (). Day return Year,month,day,weekday def modify_filename (self,filename,newname): Filepa Th,tempfilename = os.path.split (filename) print (filepath) print (tempfilename) os.rename (filepath+ ' \ \ ' +tempfilename,filepath+ ' \ \ ' +newname) print (' modified successfully! ') fo = hello () sum,year,month,day = Fo.which_day () print (year, ' Years ', month, ' Month ', day, ' th ' is the year's first ', Sum, ' Day ') #修改文件名称 (year1, MONTH1,DAY1,WEEKDAY1) = Fo.get_now_time () print (Type (year1)) newname = ' Output_ ' +str (year1) + '-' +STR (montH1) + '-' +str (day1) + '-' +str (weekday1) + ' txt ' fo.modify_filename (R ' D:\python\learn\output_1981.10.21.txt ', newname) 

Python one-day question (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.