试图获取symlink的修改时间时出现“无此类文件”错误

2024-05-04 13:01:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试获取硬盘上文件的修改时间。代码如下所示:

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不能计算出来呢


Tags: pathsupportapplicationos错误googlelibrary时间