python 获取指定目录,指定时间下所有更改的文件

来源:互联网 发布:如何找淘宝客推广 编辑:程序博客网 时间:2024/05/18 02:35
#!/usr/bin/python# -*- coding: UTF-8 -*-import osimport timeimport datetime"""    获取指定时间,指定文件夹下修改的文件"""def transTime(assignTime):    """    @summary:将给定时间转换为长整形    @param assignTime:给定的时间     如:'2016-12-3 10:30'    @return: timeLong 长整形时间    """    timeList = assignTime.replace(' ','-').replace(':','-').split('-')    timeList = map(int,timeList)  #[2016, 12, 3, 10, 30]    timeStr = datetime.datetime(*timeList) #2016-12-03 10:30:00    timeLong = time.mktime(timeStr.timetuple()) #1480732200.0    return timeLongdef getChangedFiles(assignPath,assignTime):    """    @summary: 得到指定时间之后,指定路径下(包括子路径)更改的所有文件    @param assignPath:  指定文件夹    @param assignTime: 指定时间    """    for root,dirs,files in os.walk(assignPath):        for file in files:            f = os.path.join(root,file)            mtime = os.path.getmtime(f)            if os.path.splitext(f)[1] in ('.html','.php','.py') and mtime > transTime(assignTime):                print f,time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(mtime))if __name__ == '__main__':    assignTime = '2016-12-3 10:30'   #指定时间    currentPath = os.getcwd() #当前目录    assignPath = os.path.dirname(currentPath) #当前目录的上一级目录夹    getChangedFiles(assignPath,assignTime)

0 0
原创粉丝点击