目录
一.获取文件创建时间、修改时间
打印完后可以查看该文件属性的创建时间、修改时间
import time
import os
#Read fime name
FileName=r"E:\1.png"
#print file creation time
print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(FileName).st_ctime)))
#print file modified time
print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(FileName).st_mtime)))
当需要检测某个文件是否更新时,可以对文件修改时间进行比对
import time
import os
time1 = 0
while True:
FileName = r"D:\PythonWorkSpace\chenbang\1.jpg"
time2=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.stat(FileName).st_mtime))
if time1 != time2:
time1 = time2
print('检测到文件更新')
else:
print('文件未更新')
运行结果:
D:\Python3.8.6\python.exe D:/PythonWo