ayuLiao
10/13/2018 - 4:43 AM

python读文件的创建修改时间

python读文件的创建修改时间

import time
import datetime
import os

def timestamptotime(timestamp):
    '''
    时间戳转时间
    :param timestamp: 时间戳
    :return: 时间
    '''
    timestruct = time.localtime(timestamp)
    return time.strftime('%Y-%m-%d %H:%M:%S', timestruct)

def get_filesize(filepath):
    '''
    获取文件大小,结果保留两位小数
    :param filepath: 
    :return: 
    '''
    filepath = unicode(filepath,'utf-8')
    fsize = os.path.getsize(filepath)
    fsize = fsize/float(1024*1024)
    return round(fsize,2)

def get_fileaccesstime(filepath):
    '''
    获取文件访问时间
    :param filepath: 
    :return: 
    '''
    filepath = unicode(filepath, 'utf-8')
    t = os.path.getatime(filepath)
    return timestamptotime(t)

def get_filecreatTime(filepath):
    '''
    获取文件创建时间
    :param filepath: 
    :return: 
    '''
    filepath = unicode(filepath, 'utf-8')
    t = os.path.getctime(filepath)
    return timestamptotime(t)

def get_filemodifytime(filepath):
    '''
    获取文件修改时间
    :param filepath: 
    :return: 
    '''
    filepath = unicode(filepath, 'utf-8')
    t = os.path.getmtime(filepath)
    return timestamptotime(t)