python每日一题:文件的备份与查找

题目:查找下列文件,列出所有的文件名,并列出最近一次修改的文件,和最早建立的文件。 

 

  先列代码,仅仅作为练习。

import os,time
def commax(value):## 比较时间函数,返回最大数值的序号
    if value[0] > value[1]:
        t = 0
    else:
        t = 1
    if value[t] <value[2]:
        t = 2
    return t

def commin(value):## 比较时间函数,返回数值最小数值序号
    if value[0] <value[1]:
        t = 0
    else:
        t = 1
    if value[t] >value[2]:
        t = 2
    return t

source="c:\\aa"
files=os.listdir(source)#列出aa目录下所有的文件
file0=os.stat(source+os.sep+files[0])#列出第一个文件的所有属性,一共10个,包括st_mtime和st_ctime
file1=os.stat(source+os.sep+files[1])
file2=os.stat(source+os.sep+files[2])
file=[file0,file1,file2]
a=[file[0][8],file[1][8],file[2][8]]#找出st_mtime的数值
b=[file[0][9],file[1][9],file[2][9]]#找出st_ctime的数值
print( "The searching result is:")
print( "The file sum:",len(files))
for num in  range(len(files)):
    print(num,":",files[num],",","the modifed time is:",file[num][8],"the created time is:",file[num][9],)
print("After inqurying, the recent modifed file is:",files[commax(a)])
print("the latest created file is:",files[commin(b)],)

 编译结果如下:

The searching result is:
The file sum: 3
0 : dt1000 , the modifed time is: 1543491750 the created time is: 1543489504
1 : dt1000 - 副本 , the modifed time is: 1543495031 the created time is: 1543495031
2 : dt1000 - 副本 (2) , the modifed time is: 1543495108 the created time is: 1543495093
After inqurying, the recent modifed file is: dt1000 - 副本 (2)
the latest created file is: dt1000

由于代码比较简单,不做详细描述

posted @ 2018-11-29 20:53  fjc0000  阅读(169)  评论(0编辑  收藏  举报