Python - 读取最近添加或修改的excel文件(或其他格式)

时间:2017-04-20 15:03:30

标签: python excel

如何读取最近添加或修改的某个文件夹中的excel文件?假设该文件夹包含其他excel文件或PDF,MS Words等。

如果我想阅读最新的word文档怎么办?

1 个答案:

答案 0 :(得分:3)

要做的第一件事是获取目录中所有文件(不包括文件夹)的列表。

files = [f for f in os.listdir('.') if not os.path.isdir(f)]

有了这个,你就可以做各种各样的事情。

想要获取列表PDF文件,最近修改过吗?

recentPDFs = [f for f in files if f[-4:] == '.pdf'] # Checks the file extension is pdf
recentPDFs.sort(key = lambda f: os.path.getmtime(f), reverse = True) # Sorts by time modified

excel文件怎么样,最近最少创建?

recentExc = [f for f in files if f[-5:] == '.xlsx']
recentExc.sort(key = lambda f: os.path.getctime(f))

您现在可以看到模式。

相关问题