温馨提示×

Python中path函数获取路径最后修改时间的方法

小樊
122
2024-08-23 11:02:28
栏目: 编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

要获取路径的最后修改时间,可以使用os.path.getmtime()函数来获取。示例代码如下:

import os
import time

path = "/path/to/your/file"

# 获取路径的最后修改时间
last_modified_time = os.path.getmtime(path)

# 将时间戳转换为可读的时间格式
formatted_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_modified_time))

print("文件的最后修改时间为:", formatted_time)

在上面的代码中,首先使用os.path.getmtime()函数获取路径的最后修改时间的时间戳。然后使用time.strftime()函数将时间戳转换为可读的时间格式。最后打印出文件的最后修改时间。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:Python os函数中如何获取文件的最后修改时间

0