我正在尝试获取硬盘上文件的修改时间。代码如下所示:
mtime = int(os.path.getmtime(path))
然而,我一直遇到这个奇怪的错误:
File "./import.py", line 67, in find
mtime = int(os.path.getmtime(path))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.py", line 62, in getmtime
return os.stat(filename).st_mtime
OSError: [Errno 2] No such file or directory: '/Users/username/Library/Application Support/Google/Chrome/RunningChromeVersion'
这很奇怪,因为该项确实存在;它是安装的Chrome版本的符号链接:
$ ll '/Users/username/Library/Application Support/Google/Chrome/RunningChromeVersion'
lrwxr-xr-x@ 1 username staff 13B Jan 12 16:58 /Users/username/Library/Application Support/Google/Chrome/RunningChromeVersion -> 79.0.3945.117
值得注意的是,它确实有一个修改时间Jan 12 16:58
但是,如果获得绝对路径,则它如下所示:
$ greadlink -f '/Users/username/Library/Application Support/Google/Chrome/RunningChromeVersion'
/Users/username/Library/Application Support/Google/Chrome/79.0.3945.117
如果你检查一下,这个目标实际上并不存在
$ ll '/Users/username/Library/Application Support/Google/Chrome/79.0.3945.117'
ls: /Users/username/Library/Application Support/Google/Chrome/79.0.3945.117: No such file or directory
我不明白的是,为什么Python不返回符号链接的修改时间,即它实际给出的路径?为什么这会导致错误呢?显然ls
能够看到修改时间,那么为什么Python不能计算出来呢
要获取链接本身而不是其目标的统计数据,请使用
os.lstat()
而不是os.stat()
相关问题 更多 >
编程相关推荐