python時間轉換及檔案獲取大小,建立時間和訪問時間等獲取
import time
import datetime
import os
1、 '''把時間戳轉化為時間: 1479264792 to 2016-11-16 10:53:12'''
def TimeStampToTime(timestamp):
timeStruct = time.localtime(timestamp)
return time.strftime('%Y-%m-%d %H:%M:%S',timeStruct)
“時間轉時間戳”
def TimeToTimeStamp():
timeArr = datetime.datetime.now()
TimeStamp = time.mktime(timeArr)
returnTimeStamp
2、 '''獲取檔案的大小,結果保留兩位小數,單位為MB'''
def get_FileSize(filePath):
filePath = unicode(filePath,'utf8')
fsize = os.path.getsize(filePath)
fsize = fsize/float(1024*1024)
return round(fsize,2)
3、 '''獲取檔案的訪問時間'''
def get_FileAccessTime(filePath):
filePath = unicode(filePath,'utf8')
t = os.path.getatime(filePath)
return TimeStampToTime(t)
4、 '''獲取檔案的建立時間'''
def get_FileCreateTime(filePath):
filePath = unicode(filePath,'utf8')
t = os.path.getctime(filePath)
return TimeStampToTime(t)
5、 '''獲取檔案的修改時間'''
def get_FileModifyTime(filePath):
filePath = unicode(filePath,'utf8')
t = os.path.getmtime(filePath)
return TimeStampToTime(t)