例如:
import os, datetime
def get_path_list(arg):
app_list = []
for file in os.listdir(arg):
if '.jar' in file:
app_list.append(file)
print app_list
if __name__ == '__main__':
arg = '/home/xyz/test'
get_path_list(arg)
这是python代码,用于在测试目录中查找所有jar文件,但如何根据修改日期时间对所有jar文件进行排序。
最佳答案
首先,要检查文件格式,不能使用in
操作数,可以使用endswith
或fnmatch
,然后使用os.stat().st_ctime
对文件列表进行排序:
import os, datetime
from fnmatch import fnmatch
def get_path_list(arg):
app_list = []
for file in os.listdir(arg):
if fnmatch(file,'*.jar'):
app_list.append(file)
print sorted(app_list,key=lambda f:os.stat(f).st_ctime)