Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
Last active October 13, 2018 04:50
Show Gist options
  • Select an option

  • Save ayuLiao/6ae9d01d40f2b697cb2845e0c1532ceb to your computer and use it in GitHub Desktop.

Select an option

Save ayuLiao/6ae9d01d40f2b697cb2845e0c1532ceb to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment