Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85944 > unrolled thread
| Started by | Jason Friedman <jsf80238@gmail.com> |
|---|---|
| First post | 2015-02-19 22:13 -0700 |
| Last post | 2015-02-21 00:56 +1300 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Jason Friedman <jsf80238@gmail.com> |
|---|---|
| Date | 2015-02-19 22:13 -0700 |
| Subject | Re: What behavior would you expect? |
| Message-ID | <mailman.18907.1424409195.18130.python-list@python.org> |
[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.
[toc] | [next] | [standalone]
| From | Gregory Ewing <greg.ewing@canterbury.ac.nz> |
|---|---|
| Date | 2015-02-21 00:56 +1300 |
| Message-ID | <ckolngFsb91U1@mid.individual.net> |
| In reply to | #85944 |
Jason Friedman wrote:
> 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.
It doesn't have to -- you can give it an absolute path:
>>> from glob import glob
>>> glob("/usr/include/std*.h")
['/usr/include/stdarg.h', '/usr/include/stdbool.h', '/usr/include/stddef.h',
'/usr/include/stdint.h', '/usr/include/stdio.h', '/usr/include/stdlib.h']
So just prepend the dir to the pattern and then
pass it to glob.
--
Greg
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web