简体   繁体   English

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

[英]How to get the latest folder in a directory using Python

I need to retrieve the directory of the most recently create folder.我需要检索最近创建的文件夹的目录。 I am using a program that will output a new run## folder each time it is executed (ie run01, run02, run03 and so on).我正在使用一个程序,每次执行时都会输出一个新的 run## 文件夹(即 run01、run02、run03 等)。 Within any one run## folder resides a data file that I want analyze (file-i-want.txt).在任何一个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')

In short I want to skip having to hardcode in run## and just get the directory of a file within the most recently created run## folder.简而言之,我想跳过必须在run##中进行硬编码,而只需获取最近创建的run##文件夹中的文件目录。

You can get the creation date with os.stat您可以使用os.stat获取创建日期

path = '/a/b/c'

#newest

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)

glob.glob('run*') will return the list of files/directories that match the pattern ordered by name. glob.glob('run*') 将返回与按名称排序的模式匹配的文件/目录列表。

so if you want the latest run your code will be:所以如果你想要最新的运行你的代码将是:

import glob
print(glob.glob('run*')[-1])  # raises index error if there are no runs

IMPORTANT, the files are ordered by name, in this case, for example, 'run21' will come AFTER 'run100', so you will need to use a high enough number of digits to not see this error.重要的是,文件按名称排序,例如,在这种情况下,'run21' 将在 'run100' 之后出现,因此您需要使用足够多的数字才能看到此错误。 or just count the number of matched files and recreate the name of the folder with this number.或者只是计算匹配文件的数量,然后用这个数字重新创建文件夹的名称。

you can use glob to check the number of files with the same name pattern:您可以使用 glob 检查具有相同名称模式的文件数:

import glob
n = len(glob.glob('run*')) # number of files which name starts with 'run'
new_run_name = 'run' + str(n)

Note: with this code the file names starts from 0, if you want to start from 1 just add 1 to n.注意:使用此代码,文件名从 0 开始,如果您想从 1 开始,只需将 1 添加到 n。

if you want always double digit run number (00, 01, 02) instead of 'str(n)' use 'str(n).zfill(2)'如果你想要总是两位数的运行号 (00, 01, 02) 而不是 'str(n)' 使用 'str(n).zfill(2)'

example:例子:

import glob
n = len(glob.glob('run*')) # number of files which name starts with 'run'
new_run_name = 'run' + str(n + 1).zfill(2)

pathlib is the recommeded over os for filesystem related tasks. pathlib是推荐用于文件系统相关任务的os

reference参考

You can try:你可以试试:

filepath = Path(__file__).parent / 'data/directory'
fnames = sorted(list(Path(filepath).rglob('file-i-want.txt')), key=lambda x: Path.stat(x).st_mtime, reverse=True)
filepath = str(fnames[0])
filepath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 从 S3 获取最新的文件夹路径 - How to get latest folder path from S3 using python 如何使用python获取最新创建的文件名而不是文件夹名? - How to get the latest created file name and not the folder name using python? 使用python获取每个目录中的最新文件 - Get the latest file in every directory using python Python获取最新目录并解压缩 - Python get latest directory and unzip 在目录中创建的最新文件夹的Python名称 - Python name of latest folder created in a directory 如何使用python获取文件夹中最近六个小时的最新文件更新 - How to get the latest file update in last six hours in a folder using python 如何在 Linux 中获取包含感兴趣的特定文件的最新文件夹,并在 Python 中使用 Paramiko 下载该文件? - How to get the latest folder that contains a specific file of interest in Linux and download that file using Paramiko in Python? 如何通过阅读python中的html目录使用“urllib2”获取最新文件 - How to get latest file using “urllib2” by reading html directory in python 使用 python 获取目录中包含最新时间戳的文件名 - Get the file name which contains the latest timestamp in a directory using python 如何在 python 项目中获取更高的目录文件夹? - How to get higher directory folder in python project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM