Python 获取文件或者文件夹的修改日期 获取文件的修改时间、访问时间、创建时间、大小占用

Windows、Linux

# -*- 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))

    原文作者:XD742971636
    原文地址: https://blog.csdn.net/x1131230123/article/details/122948133
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞