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: 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: References: Date: Thu, 19 Feb 2015 22:13:07 -0700 Subject: Re: What behavior would you expect? From: Jason Friedman To: python-list 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --f46d043be12aa664a9050f7e1b38 Content-Type: text/plain; charset=UTF-8 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. --f46d043be12aa664a9050f7e1b38 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I have need to search a directory and return the name of t= he most recent file matching a given pattern.=C2=A0 Given a directory with = these files and timestamps,

q.pattern1.abc Feb 13
r.p= attern1.cdf=C2=A0 Feb 12
s.pattern1.efg=C2=A0 Feb 10
t.pattern2= .abc Feb 13
u.pattern2.xyz=C2=A0 Feb 14
v.pattern2.efg=C2=A0 Feb 10
calling my_function("/path/to/dir", "pattern1&qu= ot;) will return q.pattern1.abc and calling my_function("/path/to/dir&= quot;, "pattern2") will return u.pattern2.xyz.

My qu= estion is, what would be a reasonable behavior/result/return value if:
<= /div>1. "/path/to/dir" does not exist or is not readable
2. no files match the given pattern

Also, what would be a rea= sonable name for such a function?

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

im= port glob
import os

def order_matching_files(a_path, a_glob=3D&qu= ot;*"):
=C2=A0=C2=A0=C2=A0 """Search a path for file= s whose names match a_glob
=C2=A0=C2=A0=C2=A0 and return a list of the f= ull path to such files, in descending
=C2=A0=C2=A0=C2=A0 order of modifi= cation time. Ignore directories."""
=C2=A0=C2=A0=C2=A0 pr= evious_dir =3D os.getcwd()
=C2=A0=C2=A0=C2=A0 os.chdir(a_path)
=C2=A0= =C2=A0=C2=A0 return_list =3D [os.path.join(a_path, x) for x in glob.glob(a_= glob) if os.path.isfile(x)]
=C2=A0=C2=A0=C2=A0 os.chdir(previous_dir)=C2=A0=C2=A0=C2=A0 return reversed(sorted(return_list, key=3Dos.path.getmt= ime))

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