简体   繁体   English

如何获取上次在Python中修改文件的时间?

[英]How do I get the time a file was last modified in Python?

Assuming the file exists (using os.path.exists(filename) to first make sure that it does), how do I display the time a file was last modified? 假设文件存在(使用os.path.exists(filename)首先确保它存在),如何显示文件上次修改的时间? This is on Linux if that makes any difference. 这是在Linux上,如果这有任何区别。

>>> import os
>>> f = os.path.getmtime('test1.jpg')
>>> f
1223995325.0

since the beginning of (epoch) 自(epoch)开始

os.stat() os.stat()

import os
filename = "/etc/fstab"
statbuf = os.stat(filename)
print("Modification time: {}".format(statbuf.st_mtime))

Linux does not record the creation time of a file ( for most fileystems ). Linux不记录文件的创建时间( 对于大多数文件系统 )。

New for python 3.4+ (see: pathlib ) python 3.4+的新功能(参见: pathlib

import pathlib

path = Path('some/path/to/file.ext')
last_modified = path.stat().st_mtime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM