python获取文件修改时间并比较_如何在python中获取文件创建和修改日期/时间?

import os, time, datetime

file = "somefile.txt"

print(file)

print("Modified")

print(os.stat(file)[-2])

print(os.stat(file).st_mtime)

print(os.path.getmtime(file))

print()

print("Created")

print(os.stat(file)[-1])

print(os.stat(file).st_ctime)

print(os.path.getctime(file))

print()

modified = os.path.getmtime(file)

print("Date modified: "+time.ctime(modified))

print("Date modified:",datetime.datetime.fromtimestamp(modified))

year,month,day,hour,minute,second=time.localtime(modified)[:-3]

print("Date modified: %02d/%02d/%d %02d:%02d:%02d"%(day,month,year,hour,minute,second))

print()

created = os.path.getctime(file)

print("Date created: "+time.ctime(created))

print("Date created:",datetime.datetime.fromtimestamp(created))

year,month,day,hour,minute,second=time.localtime(created)[:-3]

print("Date created: %02d/%02d/%d %02d:%02d:%02d"%(day,month,year,hour,minute,second))

印刷品

somefile.txt

Modified

1429613446

1429613446.0

1429613446.0

Created

1517491049

1517491049.28306

1517491049.28306

Date modified: Tue Apr 21 11:50:46 2015

Date modified: 2015-04-21 11:50:46

Date modified: 21/04/2015 11:50:46

Date created: Thu Feb 1 13:17:29 2018

Date created: 2018-02-01 13:17:29.283060

Date created: 01/02/2018 13:17:29

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值