def get_latest_folder(path):
try:
lists = os.listdir(path) # 列出目录的下所有文件和文件夹保存到lists
lists.sort(key=lambda fn: os.path.getmtime(path + "/" + fn)) # 按时间排序
file_new = os.path.join(path, lists[-1]) # 获取最新的文件保存到file_new
print('In path {} the latest folder is {}'.format(path, file_new))
except Exception as Err:
print("Exception Reason: '{}'".format(str(Err)))
finally:
return file_new
标签:Python,lists,获取,文件夹,file,path,new,os
From: https://www.cnblogs.com/cokefentas/p/16828036.html