Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Michael Selik Newsgroups: comp.lang.python Subject: Re: How to get a directory list sorted by date? Date: Sun, 15 May 2016 15:50:38 +0000 Lines: 30 Message-ID: References: <20160515061511.7c62b0e1@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 9fycCsv8DJjcqSrh9V6r5wg+Z3JbtBfmCY3Vy88ls/LA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.039 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'subject:How': 0.09; 'name)': 0.09; 'def': 0.13; 'received:io': 0.16; 'received:psf.io': 0.16; 'sorted()': 0.16; 'wrote:': 0.16; '>': 0.18; 'email addr:gmail.com>': 0.18; 'to:2**1': 0.21; 'tim': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'subject:list': 0.26; 'message-id:@mail.gmail.com': 0.27; 'sequence': 0.27; 'correct': 0.28; 'idea': 0.28; 'chase': 0.29; '15,': 0.30; 'skip:_ 10': 0.32; 'received:google.com': 0.35; 'next': 0.35; 'path': 0.35; 'item': 0.35; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'skip:o 20': 0.38; 'rather': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'subject:get': 0.81; 'otten': 0.84; 'sort.': 0.84; 'edwards': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=LWJamK44QHZzwizz7O5d/G7OD6D9hvBITrRcAFgWh/8=; b=E6Cgs0nDi7vQhjFSwimMentr8Nt4k0Vy5Xmi4SpA7els5putVEwCNibN2gAgVaH/Uk 7fk4q+PEZcS28ddrzFkCNRzNXssafNfx2kAwlYSJ7LXasRwlvDZq7mtqaVKvOttjEBis 8XWkHacUy5jIX71ikFQ4FSRBh/awq6xgx4fKCRQfqe+ADRMhh/Bs3HVK1FfFsrN8G3AM 37xuWLLciroPSQoRxex0VcAYQOX97TLoKjJzFPTgco9gkW7t6X3tMW04fFyFD8rJeQ5U X8sudBRlc/2st4KOJm5SkR3218TicUGLK3w/UnXtz50NNnWCAnc/kHjlk0NSYca9x+t2 o5tA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=LWJamK44QHZzwizz7O5d/G7OD6D9hvBITrRcAFgWh/8=; b=Q3GKVEvof4dyG8D5ovdOqXuy5UNRl/ZyXlTNyCstLFWUTykjXVmfvU0m3RqymmGeD5 L8Q1UoQ1/7cUKXDkRVnTZq1u0xsKLWHxTA6jegSzh1ve4c37+kjUCgTltTOG+ferqREw RJFZ4TkxpfVPyo4Kj3QKNdEU+YKpOxEQoETwGhkX+g8cZ/e6s3RBuNRcM78Anb/XzCkp MB/bMjihmbI0uJGISKRpAHoJGckTdV7yQJ4ZX641Lr9HpSHVxdsh1yecInliI9x5oY+0 6mX9qhYwCGjuP6hbaQWL42OGfQ6ANNHs0CAeASnmph/lPNa42UIXILX8NsaKST278Ppy AJFA== X-Gm-Message-State: AOPr4FWnFAFZmnAxYlrzYhnvx4IorPBjxqJzafzsSmtaiAX3wjYYdiuyG/ii7j2DVpnLgew09Og+9jvzCTZmfw== X-Received: by 10.140.92.65 with SMTP id a59mr24183267qge.93.1463327448363; Sun, 15 May 2016 08:50:48 -0700 (PDT) In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <20160515061511.7c62b0e1@bigbox.christie.dr> Xref: csiph.com comp.lang.python:108640 On Sun, May 15, 2016, 10:37 AM Grant Edwards wrote: > On 2016-05-15, Tim Chase wrote: > > On 2016-05-15 11:46, Peter Otten wrote: > >> def sorted_dir(folder): > >> def getmtime(name): > >> path = os.path.join(folder, name) > >> return os.path.getmtime(path) > >> > >> return sorted(os.listdir(folder), key=getmtime, reverse=True) > >> > >> The same idea will work with pathlib and os.scandir(): > >> > >> def _getmtime(entry): > >> return entry.stat().st_mtime > >> > >> def sd_sorted_dir(folder): > >> return sorted(os.scandir(folder), key=_getmtime, reverse=True) > > > > unless sorted() returns a lazy sorter, > > What's a lazy sorter? > One that doesn't calculate the next item in the sequence until you ask for it. It's impossible unless you don't mind an approximation rather than correct sort. >