简体   繁体   中英

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). 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

Would something like os.stat work better?

尝试:

newest = max(glob.iglob('Directory Name/*'), key=os.path.getctime)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM