我有一个sym链接文件目录,如下所示。
如何从目录
获取最后创建的文件lrwxrwxrwx 1 cha mux 46 Apr 30 03:39 load-16.29-40 -> ../../../build-150429/swp/latest/load-16.29-40
lrwxrwxrwx 1 cha mux 46 Apr 30 21:36 load-16.30-40 -> ../../../build-150430/swp/latest/load-16.30-40
lrwxrwxrwx 1 cha mux 45 May 3 22:58 load-17.2-40 -> ../../../build-150502/swp/latest/load-17.2-40
lrwxrwxrwx 1 cha mux 45 May 5 01:39 load-17.4-40 -> ../../../build-150504/swp/latest/load-17.4-40
lrwxrwxrwx 1 cha mux 45 May 6 00:58 load-17.5-40 -> ../../../build-150505/swp/latest/load-17.5-40
lrwxrwxrwx 1 cha mux 45 May 7 03:19 load-17.6-10 -> ../../../build-150506/swp/latest/load-17.6-10
输出应为" load-17.6-10"
答案 0 :(得分:0)
这是文件列表:
os.listdir(dir)
只是符号链接:
[name for name in os.listdir(dir) if os.path.islink(name)]
最新链接:
max([name for name in os.listdir(dir) if os.path.islink(name)],
key = lambda f: os.lstat(os.path.join(dir, f)).st_ctime)
此处使用os.lstat()
:它的工作原理与os.stat()
类似,但不遵循符号链接。