您的位置:首页 > 编程语言 > Python开发

python 时间格式 及 转换 文件时间说明

2017-09-06 20:18 471 查看
#coding: utf-8
__author__ = 'KK'
import time

#获取当前时间的字符串形式 Wed Sep 06 20:14:46 2017
print time.ctime()
#获取当前时间戳 1504700086.44
print time.time()
#当前时间的struct_time格式 time.struct_time(tm_year=2017, tm_mon=9, tm_mday=6, tm_hour=20, tm_min=14, tm_sec=46, tm_wday=2, tm_yday=249, tm_isdst=0)
print time.localtime()
# 当前时间格式化为字符串 2017_09_06 20:14:46
print time.strftime("%Y_%m_%d %H:%M:%S")


时间转换:

import os
import time
import datetime
#获取文件的创建时间
file ="D:\\log.txt"
timestamp = os.path.getmtime(file)
#1504700941.93
print timestamp
#time.struct_time(tm_year=2017, tm_mon=9, tm_mday=6, tm_hour=20, tm_min=29, tm_sec=1, tm_wday=2, tm_yday=249, tm_isdst=0)
print time.localtime(timestamp)
#2017-09-06 20:29:01.934336
print datetime.datetime.fromtimestamp(timestamp)
#2017-09-06
print datetime.date.fromtimestamp(timestamp)
#time.struct_time(tm_year=2017, tm_mon=9, tm_mday=6, tm_hour=12, tm_min=29, tm_sec=1, tm_wday=2, tm_yday=249, tm_isdst=0)
print time.gmtime(os.path.getmtime(file))

#文件时间说明:
print os.path.getatime(file) #输出最近访问时间1504700920.69 win2003 SP1之后出于性能考虑该参数禁用,=创建时间
print os.path.getctime(file) #输出文件创建时间1504700920.69
print os.path.getmtime(file) #输出最近修改时间1504705313.16
print time.gmtime(os.path.getmtime(file)) #以struct_time形式输出最近修改时间
print os.path.getsize(file) #输出文件大小(字节为单位)
print os.path.abspath(file) #输出绝对路径'/Volumes/Leopard/Users/Caroline/Desktop/1.mp4'
print os.path.normpath(file) #输出规范化路径'/Volumes/Leopard/Users/Caroline/Desktop/1.mp4'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: