#有一个文件,文件名为output_1981.10.21.txt 。
# 下面使用Python: 读取文件名中的日期时间信息,并找出这一天是周几。
# 将文件改名为output_YYYY-MM-DD-W.txt
# (YYYY:四位的年,MM:两位的月份,DD:两位的日,W:一位的周几,并假设周一为一周第一天) '''
解题 思路:
1、获取文件名,截取日期 ,生成日期变量参数 '''
import os,re
import time,datetime
class hello:
def __init__(self):
print('求文件名,取出日期,算出1年中哪一天,更改文件名')
def get_date(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
#给个时间 ,找出这一天是1年中的哪一天
'''
解题 思路:
一年12个月的天数弄成一个list,只有2月份不确认,闰年28天,非闰年29天,得出2月份天数。
求前面几个月的天数之和+当月天数,就是当当年第几天
'''
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 = 28
elif(year%400==0):
num = 28
else:
num =29
month_list = [31, num, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
for i in range(0,month-1):
sum +=month_list[i]
#第几天
sum +=day
return sum,year,month,day #获取当前系统时间 年月日 周
def get_now_time(self):
now = time.localtime()
weekday = time.strftime('%w',now)
year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day
return year,month,day,weekday def modify_filename(self,filename,newname):
filepath,tempfilename = os.path.split(filename)
print(filepath)
print(tempfilename)
os.rename(filepath+'\\'+tempfilename,filepath+'\\'+newname)
print('修改成功!') fo = hello()
sum,year,month,day = fo.which_day()
print(year,'年',month,'月',day,'日是一年中的第',sum,'天')
#修改文件名称
(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一天一题(1)的更多相关文章

  1. python一天一题(3)

    #--coding=utf8-- from selenium import webdriver import time import logging import os.path ''' 搜索取搜索的 ...

  2. python一天一题(2)

    python查询mysql数据库 import pymysql host = '192.168.74.5' user = 'root' passwd ='root' port = 3310 db = ...

  3. Android面试一天一题(1Day)

    写在前面 该博客思路源于在简书看到goeasyway博主写的Android面试一天一题系列,无copy之意,仅为让自己总结知识点,成长一点点.先感谢各位大神的无私分享~! 关于题目,大部分则出自And ...

  4. Python面试真题答案或案例

    Python面试真题答案或案例如下: 请等待. #coding=utf-8 #1.一行代码实现1--100之和 print(sum(range(1,101))) #2.如何在一个函数内部修改全局变量 ...

  5. Python基础数据类型题

    Python基础数据类型 题考试时间:三个小时 满分100分(80分以上包含80分及格)1,简述变量命名规范(3分) 1.必须是字母,数字,下划线的任意组合. 2.不能是数字开头 3.不能是pytho ...

  6. 『Python题库 - 填空题』151道Python笔试填空题

    『Python题库 - 填空题』Python笔试填空题 part 1. Python语言概述和Python开发环境配置 part 2. Python语言基本语法元素(变量,基本数据类型, 基础运算) ...

  7. Python练习100题

    Python练习100题 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #Filename:001.py cnt = 0#count the sum of res ...

  8. 21天学通Python课后实验题4.6

    21天学通Python课后实验题4.6 1. 编程实现用户输入一门课程的两门子课程成绩,第一门子课程60分以上,则显示“通过”,第一门子课程不及格,则显示“未通过”,第一门子课程及格,而第二门子课程不 ...

  9. Python语言上机题实现方法(持续更新...)

    Python语言上机题实现方法(持续更新...) 1.[字符串循环左移]给定一个字符串S,要求把S的前k个字符移动到S的尾部,如把字符串"abcdef"前面的2个字符'a'.'b' ...

随机推荐

  1. Oracle根据表生成系统流水号

    1.建表tablewater create table TABLEWATER ( tb_id INTEGER not null, vc_table_name ), num_water_no ) )vc ...

  2. RHEL7 富规则删除

    1. 查看已经创建好的富规则 firewall-cmd --list-rich-rules 2. 删除富规则(******代表已经创建好的完整富规则内容) firewall-cmd --remove- ...

  3. [Windows Azure] Adding Sign-On to Your Web Application Using Windows Azure AD

    Adding Sign-On to Your Web Application Using Windows Azure AD 14 out of 19 rated this helpful - Rate ...

  4. linux命令(53):用户和用户组

    Linux 用户和用户组详细解说 本文主要讲述在Linux 系统中用户(user)和用户组(group)管理相应的概念: 用户(user)和用户组(group)相关命令的列举: 其中也对单用户多任务, ...

  5. python(40):利用utf-8编码判断中文英文字符

    #!/usr/bin/env Python # -*- coding:GBK -*- """汉字处理的工具: 判断unicode是否是汉字,数字,英文,或者其他字符. 全 ...

  6. (转)Go和HTTPS

    转自:http://studygolang.com/articles/2946 Go和HTTPS  2015-04-30   bigwhite  阅读 5688 次   4 人喜欢  3 条评论  收 ...

  7. 使用FastJson从json串中根据key获取value

    import com.alibaba.fastjson.JSONObject; /** * Created by SYJ on 2017/9/13. */ public class MainTest ...

  8. python dict与list

    本文实例讲述了python中字典(Dictionary)用法.分享给大家供大家参考.具体分析如下: 字典(Dictionary)是一种映射结构的数据类型,由无序的“键-值对”组成.字典的键必须是不可改 ...

  9. OpenStack云计算(一)——OpenStack 网络

    关于OpenStack OpenStack它是 Rackspace Cloud 和 NASA 负责的一个开源基础架构即服务的云计算项目. OpenStack 是一个由开发者和云计算技术人员的全球协作开 ...

  10. Pyperclip – A cross-platform clipboard module for Python

    Usage is simple: import pyperclip pyperclip.copy('The text to be copied to the clipboard.') spam = p ...