Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'error:': 0.07; 'subject:would': 0.07; 'string': 0.09; 'raises': 0.09; 'valueerror:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '"does': 0.16; '-tkc': 0.16; 'arg': 0.16; 'cares': 0.16; 'filenames.': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterable:': 0.16; 'wrote:': 0.18; 'skip:f 30': 0.19; 'seems': 0.21; 'import': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'cc:2**0': 0.24; 'holds': 0.26; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; "doesn't": 0.30; 'skip:g 30': 0.30; 'gives': 0.31; '"",': 0.31; 'asks': 0.31; 'file': 0.32; 'probably': 0.32; '(most': 0.33; 'test': 0.35; 'but': 0.35; 'sequence': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'list': 0.37; 'received:10': 0.37; 'skip:o 20': 0.38; 'jason': 0.38; 'question,': 0.38; 'rather': 0.38; 'recent': 0.39; 'simply': 0.61; "you're": 0.61; 'to:addr:gmail.com': 0.65; 'directories:': 0.84; 'subject:you': 0.87; 'directly.': 0.95; 'silent': 0.95 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1424614561569:82315289 X-MC-Ingress-Time: 1424614561569 Date: Sun, 22 Feb 2015 08:17:36 -0600 From: Tim Chase To: Jason Friedman Subject: Re: What behavior would you expect? In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AuthUser: tim@thechases.com Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424615164 news.xs4all.nl 2896 [2001:888:2000:d::a6]:38133 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86125 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 "", line 1, in ValueError: max() arg is an empty sequence -tkc