过滤上次修改时间的文件

问题描述

我有一个文件夹,其中包含每 1 分钟间隔生成的 csv 文件。我想过滤在特定时间(例如下午 12:15)之前到达的文件。我的代码如下:

    import os
    import pandas as pd
    
    search_dir = r"C:\Users\123\Documents\Folder"
    os.chdir(search_dir)
    files = filter(os.path.isfile,os.listdir(search_dir))
    files = [os.path.join(search_dir,f) for f in files] # add path to each file
    files = files.sort(key=lambda x: os.path.getmtime(x),reverse=True)

这里有按上次修改时间排序的文件列表。任何有关如何过滤在特定时间之前到达的文件的帮助。

解决方法

您是否已经检查过此答案 python filter files by modified time?您的要求应该对此稍作修改。

import os
import pandas as pd
from datetime import datetime
from pathlib import Path

search_dir = r"C:\Users\123\Documents\Folder"
os.chdir(search_dir)
files = filter(os.path.isfile,os.listdir(search_dir))
files = [os.path.join(search_dir,f) for f in files] # add path to each file

到这里为止,您的代码保持不变。如果以后要过滤文件,我不太确定为什么需要按时间对文件进行排序。然而,假设这是一个必要的步骤,我已经更改了最后一行,因为它导致 NoneType 结果。相反,我使用 pathlib 库根据需要对文件进行排序。所以用下面一行替换最后一行。

files_sorted = sorted(Path(search_dir).iterdir(),key=os.path.getmtime)

您尚未指定过滤时间是用户提供的还是文件中的时间戳。如果它是来自文件的时间戳,则通过调用该文件的时间戳继续。例如,我从排序后的文件列表中取第一个文件的时间。

particular_time = os.path.getmtime(files_sorted[0])

接下来,假设您要删除所有时间低于特定时间的文件(您没有再次明确提及您想要的内容),请执行以下操作:

for f in files_sorted:
    tLog = os.path.getmtime(f)
    print("checking ",f,datetime.fromtimestamp(tLog))

    if particular_time > tLog:
        print("filter out the files",f)
        files_sorted.remove(f)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...