Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86125
| Date | 2015-02-22 08:17 -0600 |
|---|---|
| From | Tim Chase <python.list@tim.thechases.com> |
| Subject | Re: What behavior would you expect? |
| References | (1 earlier) <CANy1k1gMgGnqNcgTwjcqfYH-QctZthv89uXcU8SV9Uk6Wji=4A@mail.gmail.com> <CAPTjJmpeYwsQ=XLu5WicVEnqMZpvKJueZRrM9WGafcEm1BFatg@mail.gmail.com> <CANy1k1j8ni7TtZPq_a2r8TUvY13rh0oY8xt-t6+9KPxwfAYEyw@mail.gmail.com> <CAPTjJmpq3t4qb+VD+uL6w+=_wf5v0BFThYFM+a8JAe2RDXpTYg@mail.gmail.com> <CANy1k1hzJ7gr2n2rGMzTV7i0Dj-sRv5xddoQwtp-svO6BbG0Vg@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.19017.1424615164.18130.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: What behavior would you expect? Tim Chase <python.list@tim.thechases.com> - 2015-02-22 08:17 -0600
csiph-web