如何在Python中獲得文件創(chuàng)建和修改日期/時間?我有一個腳本,需要根據(jù)文件創(chuàng)建和修改日期做一些事情,但是必須在Linux和Windows上運行。什么是最好的跨平臺在Python中獲取文件創(chuàng)建和修改日期/時間的方法?
3 回答

SMILET
TA貢獻1796條經(jīng)驗 獲得超4個贊
os.path.getmtime
os.path.getctime
import os.path, timeprint("last modified: %s" % time.ctime(os.path.getmtime(file)))print("created: %s" % time.ctime(os.path.getctime(file)))
os.stat
:
import os, time(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)print("last modified: %s" % time.ctime(mtime))
注: ctime()

慕尼黑的夜晚無繁華
TA貢獻1864條經(jīng)驗 獲得超6個贊
os.stat(filename).st_mtime
.
datetime
import osimport datetimedef modification_date(filename): t = os.path.getmtime(filename) return datetime.datetime.fromtimestamp(t)
>>> d = modification_date('/var/log/syslog')>>> print d2009-10-06 10:50:01>>> print repr(d)datetime.datetime(2009, 10, 6, 10, 50, 1)
添加回答
舉報
0/150
提交
取消