Python os.path.getatime()方法:获取文件最后访问时间

在Python编程中,我们经常需要获取文件的相关信息,例如创建时间、修改时间以及访问时间。os.path.getatime() 方法可以帮助我们轻松获取指定路径的最后访问时间。

1. 函数定义

os.path.getatime(path)

2. 参数说明

  • path:要获取最后访问时间的文件路径,可以是字符串类型。

3. 返回值

  • 返回一个浮点数,表示最后访问时间的时间戳,以秒为单位。

4. 代码示例

以下代码演示了如何使用 os.path.getatime() 方法获取文件最后访问时间,并使用 time.ctime() 方法将其转换为更易读的格式:

import os.path
import time

# 获取文件的最后访问时间
path = 'example.txt'
atime = os.path.getatime(path)
print('最后访问时间:', time.ctime(atime))

5. 输出示例

最后访问时间: Mon Sep 20 12:34:56 2021

注意事项

  • os.path.getatime() 方法返回的时间戳精度取决于操作系统。
  • 如果文件不存在或无法访问,将引发 FileNotFoundErrorOSError 异常。

希望本文能够帮助您理解和使用 os.path.getatime() 方法。如果您有任何问题,请随时提出。

Python os.path.getatime()方法:获取文件最后访问时间详解

原文地址: https://www.cveoy.top/t/topic/eHYz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录