如何使用Python获取目录中的最新文件夹

问题描述 投票:0回答:1

我需要检索最近创建的文件夹的目录。我正在使用一个程序,该程序每次执行时都会输出一个新的run ##文件夹(即run01,run02,run03等)。在任何run##文件夹中都存在一个我要分析的数据文件(file-i-want.txt)。

folder_numb = 'run01'
dir = os.path.dirname(__file__)
filepath = os.path.join(dir, '..\data\directory',run_numb,'file-i-want.txt')

简而言之,我想跳过不必在run##中进行硬编码,而只是获取最近创建的run##文件夹中的文件目录。

python filepath python-os
1个回答
0
投票
path = '/a/b/c' #newest max([f for f in os.listdir(path)], key=lambda x: os.stat(os.path.join(path,x)).st_birthtime) # all files sorted sorted_files = sorted([f for f in os.listdir(path)],key=lambda x: os.stat(os.path.join(path, x)).st_birthtime, reverse=True)

并通过]获得最新信息>

© www.soinside.com 2019 - 2024. All rights reserved.