2015-05-09 107 views
0

我有一个如下所示的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 

得到最后创建的文件输出应该是 “负载17.6-10”

回答

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(),但不跟随符号连接。

相关问题