菜鸟IT的博客 >> Python
Python 获取文件或者文件夹的修改日期 获取文件的修改时间、访问时间、创建时间、大小占用
https://blog.csdn.net/x1131230123/article/details/122948133
# ————————————
# -*- coding: UTF8 -*-
import os
import time
def TimeStampToTime(timestamp):
timeStruct = time.localtime(timestamp)
return time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)
def get_FileCreateTime(filePath):
# '''获取文件的创建时间'''
# filePath = unicode(filePath,'utf8')
t = os.path.getctime(filePath)
return TimeStampToTime(t)
def get_FileModifyTime(filePath):
# '''获取文件的修改时间'''
# filePath = unicode(filePath, 'utf8')
t = os.path.getmtime(filePath)
return TimeStampToTime(t)
def get_FileAccessTime(filePath):
# '''获取文件的访问时间'''
# filePath = unicode(filePath, 'utf8')
t = os.path.getatime(filePath)
return TimeStampToTime(t)
def get_FileSize(filePath):
# '''获取文件的大小,结果保留两位小数,单位为MB'''
# filePath = unicode(filePath,'utf8')
fsize = os.path.getsize(filePath)
fsize = fsize / float(1024 * 1024)
return round(fsize, 2)
if __name__ == '__main__':
path = r"D:\Desktop\实际图每隔30分钟\kevin_picture_alg_need_collect"
print(get_FileCreateTime(path))
print(get_FileModifyTime(path))
print(get_FileAccessTime(path))
print(get_FileSize(path))
菜鸟IT博客[2022.08.26-07:22] 访问:317