python - Python获取最新文件列表并将其与旧文件列表进行比较
问题描述
目标
每 30 分钟从目标目录获取最新文件列表
我的解决方案(如果您知道更好,请告诉我)
- 每 30 分钟从目标目录获取文件列表。
- 将当前文件与新文件列表进行比较
- 制作最新文件的新列表。
问题
- 每 30 分钟从目标目录获取文件列表。
在这里我不知道如何获取当前和 30 分钟后的列表?
这里函数返回列表。
from os import listdir
from os.path import isfile, join
def getFileNames(mypath):
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
return onlyfiles
解决方案
以下是在 Python 中每 30 分钟检查一次新文件的方法:
from os import listdir
from os.path import isfile, join
from time import sleep
def getFileNames(mypath):
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
return onlyfiles
folder = "/tmp" #location of temp files on Linux
old = getFileNames(folder)
while True:
sleep(1800) #30 minutes
new = getFileNames(folder)
newFiles = [f for f in new if f not in old]
print(newFiles)
old = new
推荐阅读
- linux - 只有当我是 sudo 时,我才能 ssh 到远程服务器
- javascript - 错误:无法从 electronjs 中的 app.createWindow 创建图像
- angular - 以角度 9 获取响应标题变量
- java - 将记录插入 Oracle DB 时,JDBC(在 Spring Data JPA 中)是否总是采用 JVM 默认时区?
- android - Android.mk 尝试访问 .so 文件
- python - 如何从 SRT 文件中提取时间?
- c++ - 如何按字符而不是代码单元(UTF-8、UTF-16)计算字符串的长度?
- r - 总结 data.table - 在 R 中按日期创建多个列子集
- python - Pytorch:反向传播多个损失
- office-js - 当切片计数超过一个 Microsoft Office Word 加载项时,file.getFileAsync 抛出 5001(内部错误)