gpt4 book ai didi

python - 列出目录中的所有 .jar 文件。在 python 中按修改日期排序

转载 作者:太空宇宙 更新时间:2023-11-04 01:02:22 25 4
gpt4 key购买 nike

例如:

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 操作数,您可以使用 endswithfnmatch 然后对文件进行排序文件列表 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)

关于python - 列出目录中的所有 .jar 文件。在 python 中按修改日期排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32246526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com