简体   繁体   English

使用 python 获取目录中包含最新时间戳的文件名

[英]Get the file name which contains the latest timestamp in a directory using python

Lets say that in a directory i have multiple files like this:可以说在一个目录中我有多个这样的文件:

Test1_2021-05-17 1139.xlsx
Test1_2021-04-17 1139.xlsx
Test1_2021-03-17 1139.xlsx
Test1_2021-02-17 1139.xlsx
Test1_2021-01-17 1139.xlsx
Test2_2021-05-17 1139.xlsx
Test2_2021-04-17 1139.xlsx
Test2_2021-03-17 1139.xlsx
Test2_2021-02-17 1139.xlsx

How can I find the file which contains the latest timestamp and then i want to open it as a data frame.如何找到包含最新时间戳的文件,然后我想将其作为数据框打开。

So, eg.所以,例如。 o want to get the file name: Test1_2021-05-17 1139.xlsx. o 要获取文件名:Test1_2021-05-17 1139.xlsx。 How can i do that with python?我怎么能用 python 做到这一点?

I tried this one but it is not getting me the file with the latest timestamp on its name:我试过这个,但它没有让我得到名称上带有最新时间戳的文件:

import glob
import os

list_of_files = glob.glob('/path/*') 
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)

If you really need to make it based on the filenames, you can pass a lamdba function to max() , to modifies the items properly:如果您确实需要根据文件名进行制作,可以将lamdba function 传递给max() ,以正确修改项目:

fNames = '''.../0010/Test1_2021-05-17 1139.xlsx
.../1212/Test1_2021-04-17 1139.xlsx
.../1212/Test1_2021-03-17 1139.xlsx
.../1444/Test1_2021-02-17 1139.xlsx
.../1212/Test1_2021-01-17 1139.xlsx
.../19/Test2_2021-05-17 1139.xlsx
.../1212/Test2_2021-04-17 1139.xlsx
.../1212/Test2_2021-03-17 1139.xlsx
.../1212/Test2_2021-02-17 1139.xlsx'''.splitlines()

# use only files containing 'Test_1':
fNames = [f for f in fNames if 'test1_' in f.lower()]

# rsplit removes the directory names.
max_fName = max(
    fNames, key=lambda p: p.rsplit('/', 1)[1].split('_', 1)[1].split(' ', 1)[0]
)
print(max_fName)

#or hard coded:
max_fName = max(fNames, key=lambda p: p.rsplit('/', 1)[1][6:16])
print(max_fName)

Out:出去:

.../0010/Test1_2021-05-17 1139.xlsx
.../0010/Test1_2021-05-17 1139.xlsx

Maybe you have to filter your filenames before:也许您必须先过滤文件名:

import pathlib
import os.path
import pandas as pd

filename = max([f for f in pathlib.Path('/path').glob('Test_*.xlsx')], 
               key=os.path.getctime)

df = pd.DataFrame(filename)

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

相关问题 使用python获取每个目录中的最新文件 - Get the latest file in every directory using python 如何使用python获取最新创建的文件名而不是文件夹名? - How to get the latest created file name and not the folder name using python? 如何使用python获取目录中的文件名 - how to get name of a file in directory using python python代码获取(最新)文件,时间戳作为附加到电子邮件的名称 - python code to get (latest) file with timestamp as name to attach to a e-mail 如何通过传递包含文件所在目录的变量名来打开文件 python? - how to open a file by passing a variable name which contains the directory where the file is located in python? 如何使用Python获取目录中的最新文件夹 - How to get the latest folder in a directory 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 / Itertools:按名称获取最新文件 - Python/Itertools: Get latest file by name 如何使用 Python 获取 kafka 主题的最新时间戳? - How to get latest timestamp for kafka topic using Python? 如何通过阅读python中的html目录使用“urllib2”获取最新文件 - How to get latest file using “urllib2” by reading html directory in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM