defget_latest_file(file_path):
lists=os.listdir(file_path)#列出目录的下所有文件和文件夹保存到lists
print(list)
#按时间排序
lists.sort(key=lambdafn:os.path.getmtime(file_path+"\\"+fn))
#获取最新的文件保存到file_new
file_new=lists[-1]
print(file_new)
returnfile_new
主函数调用:
if__name__=="__main__":
#目录地址
file_path="model"
get_latest_file(file_path)