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.