What behavior would you expect?
Tim Chase
python.list at tim.thechases.com
Sun Feb 22 09:17:36 EST 2015
On 2015-02-19 22:55, Jason Friedman wrote:
> > If you're going to call listdir, you probably want to use fnmatch
> > directly.
> >
> > fnmatch seems to be silent on non-existent directories:
> python -c 'import fnmatch; fnmatch.fnmatch("/no/such/path", "*")'
a better test would be glob.glob as fnmatch simply asks "does this
string match this pattern?" so it cares nothing for filenames.
However, it still holds that glob.glob("/does/not/exist/*.txt")
doesn't raise an error but rather just returns an empty list of
iterables.
However, for the OP's question, it's max() that raises an error:
import glob
import os
def most_recent_file(loc, pattern):
globstr = os.path.join(loc, pattern)
return max(glob.glob(globstr), key=lambda f: os.stat(f).st_mtime)
gives me this when the glob returns an empty iterable:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: max() arg is an empty sequence
-tkc
More information about the Python-list
mailing list