如何在Python中获取文件的修改日期/时间?

23

如何在Python中获取文件的修改日期/时间?

2个回答

39
os.path.getmtime(filepath)
或者
os.stat(filepath).st_mtime

1
@endolith: https://dev59.com/G3VC5IYBdhLWcg3wnCj6#237082 说 "os.path.getmtime() 就是为此而生的,而且更简单。" - endolith

20

格式化:

import time
print time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(os.path.getmtime(fname)))

类似ISO的格式:"%Y-%m-%d %I:%M:%S %p" - Gringo Suave

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接