[英]Last Modified File in Python?
So I'm trying to write a script that allows me import the most recently modified file from a directory. 因此,我正在尝试编写一个脚本,该脚本允许我从目录中导入最新修改的文件。 I've looked at the glob
and os.listdir
commands but they don't seem to do it (I get errors). 我已经看过glob
和os.listdir
命令,但它们似乎没有做到(我得到了错误)。 Any thoughts? 有什么想法吗?
import os
import glob
newest = max(glob.iglob('Directory Name'), key=os.path.getctime)
print newest
f = open(newest,'r')
I get an error: 我收到一个错误:
max() arg is an empty sequence max()arg是一个空序列
Would something like os.stat work better? 像os.stat这样的东西会更好吗?
尝试:
newest = max(glob.iglob('Directory Name/*'), key=os.path.getctime)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.