Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85944

Re: What behavior would you expect?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <jsf80238@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'skip:[ 20': 0.04; 'argument': 0.05; 'subject:would': 0.07; 'bits': 0.09; 'shame': 0.09; 'def': 0.12; 'descending': 0.16; 'function?': 0.16; 'optional': 0.16; 'os.getcwd()': 0.16; 'readable': 0.16; 'modification': 0.16; 'ignore': 0.16; 'have:': 0.19; 'feb': 0.22; 'import': 0.22; 'directory.': 0.24; 'question': 0.24; 'header:In- Reply-To:1': 0.27; 'matching': 0.30; 'message-id:@mail.gmail.com': 0.30; 'file': 0.32; 'skip:m 30': 0.32; 'to:name:python-list': 0.33; 'advice': 0.35; 'received:google.com': 0.35; 'everyone.': 0.36; 'subject:?': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'thank': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'most': 0.60; 'full': 0.61; 'name': 0.63; 'such': 0.63; 'taking': 0.65; 'timestamps,': 0.84; 'subject:you': 0.87
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=muG5cjEgmJUo7QTOFqNMNFkQt5C6N2LsgYvZAI9jayQ=; b=KuoEjtyBpH3gt12DQ5Hn9EOEgxU9Y4nA72jOgOzqqzNUC4eINFIEfnBeMx5AAT1Yoq 8V8SMNygrPt9RvUXhBMcxMObPQS3LYDyTcXN8Rgj9uFB9ab8Ec1chlmhwLvLHF6QWWhz Kppv8x+IbHoM/+e4uS+TeNzjap9+BvGoRy6cQvnY99Ao9OyupQqM4nDH5Yva74abPvdf jMqvn3vsBkBS9SCgDSTwLdz4Kt81KUfZNPsjm6pGvKxv0FEQ5j4ftCKvokur04Cmt+RJ rU1k5UkUHznsp8dpE5S4Nt04TvLNtSPTutHhs+hlWMjQkeld6LtpTa9/3FxtgNJMCYe/ lODQ==
MIME-Version 1.0
X-Received by 10.180.74.141 with SMTP id t13mr20982079wiv.45.1424409187736; Thu, 19 Feb 2015 21:13:07 -0800 (PST)
In-Reply-To <CANy1k1g+wU6VUifZpV5+DWendT2LqS-PhRA-KC5hmq2xQ_uy+A@mail.gmail.com>
References <CANy1k1g+wU6VUifZpV5+DWendT2LqS-PhRA-KC5hmq2xQ_uy+A@mail.gmail.com>
Date Thu, 19 Feb 2015 22:13:07 -0700
Subject Re: What behavior would you expect?
From Jason Friedman <jsf80238@gmail.com>
To python-list <python-list@python.org>
Content-Type multipart/alternative; boundary=f46d043be12aa664a9050f7e1b38
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18907.1424409195.18130.python-list@python.org> (permalink)
Lines 81
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1424409195 news.xs4all.nl 2895 [2001:888:2000:d::a6]:39003
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85944

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

I have need to search a directory and return the name of the most recent
file matching a given pattern.  Given a directory with these files and
timestamps,

>
> q.pattern1.abc Feb 13
> r.pattern1.cdf  Feb 12
> s.pattern1.efg  Feb 10
> t.pattern2.abc Feb 13
> u.pattern2.xyz  Feb 14
> v.pattern2.efg  Feb 10
>
> calling my_function("/path/to/dir", "pattern1") will return q.pattern1.abc
> and calling my_function("/path/to/dir", "pattern2") will return
> u.pattern2.xyz.
>
> My question is, what would be a reasonable behavior/result/return value if:
> 1. "/path/to/dir" does not exist or is not readable
> 2. no files match the given pattern
>
> Also, what would be a reasonable name for such a function?
>

Thank you everyone.  Taking bits of advice from each of you I tentatively
have:

import glob
import os

def order_matching_files(a_path, a_glob="*"):
    """Search a path for files whose names match a_glob
    and return a list of the full path to such files, in descending
    order of modification time. Ignore directories."""
    previous_dir = os.getcwd()
    os.chdir(a_path)
    return_list = [os.path.join(a_path, x) for x in glob.glob(a_glob) if
os.path.isfile(x)]
    os.chdir(previous_dir)
    return reversed(sorted(return_list, key=os.path.getmtime))

It's a shame that glob.glob does not take an arbitrary directory as an
optional argument if one does not want to scan the current directory.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: What behavior would you expect? Jason Friedman <jsf80238@gmail.com> - 2015-02-19 22:13 -0700
  Re: What behavior would you expect? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-02-21 00:56 +1300

csiph-web