python - 使用 Python 获取目录中文件列表的修改日期的最快方法是什么?
问题描述
我在一个包含大量 csv 文件(500+)的目录中有一个文件夹。我只需要在某个日期之后具有修改日期的 csv 文件。最终,我会将这些文件 pd.concat 放入一个 pandas 数据框中。
获得相关文件概览的最快方法是什么?
我目前的解决方案如下所示:
## get list of files
list_of_files = glob.glob(Path + '*.csv')
## transform to datataframe
df_files = pd.DataFrame(list_of_files, columns = ['files'])
## Calculate modification time (SLOW)
df_files['Modification_Time'] = df_files['files'].apply(lambda x: datetime.date.fromtimestamp(os.path.getmtime(x)))
## Eventually filter based on Modification date and pd.concat relevant files (code not relevant for the question)
我的解决方案相当慢,我想知道是否有更快的解决方案?
解决方案
尝试在初始化数据框之前评估修改时间 -
import glob
result= []
for file in glob.glob(Path + '*.csv'):
temp = {
'files': file,
'Modification_Time': datetime.date.fromtimestamp(
os.path.getmtime(file)
),
}
result.append(temp)
df_files = pd.DataFrame(result)
推荐阅读
- javascript - 为什么同时具有事件和数据时服务器发送事件不会在前端触发?
- node.js - 我安装了 node.js 并且无法运行“npm start”来处理 react js
- c# - 双多对多关系实体框架
- vb.net - 可执行创建。Win10、Win7和XP哪个路径兼容?
- node.js - 在客户端保存 JWT
- c++ - 为什么不建议允许查询 C++20 合约的合约级别?
- python - 提高 pandas read_csv 单行的速度
- java - 尝试更新 MainActivity 之外的 UI 元素
- c++ - NULL 在 C++ 中是如何定义的?
- c++ - MSVC C++ 编译器限制 - 特征在 CRTP 下不起作用