Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85946 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2015-02-20 16:20 +1100 |
| Last post | 2015-02-20 16:20 +1100 |
| Articles | 1 — 1 participant |
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? Chris Angelico <rosuav@gmail.com> - 2015-02-20 16:20 +1100
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-02-20 16:20 +1100 |
| Subject | Re: What behavior would you expect? |
| Message-ID | <mailman.18909.1424409635.18130.python-list@python.org> |
On Fri, Feb 20, 2015 at 4:13 PM, Jason Friedman <jsf80238@gmail.com> wrote: > 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. If you look at the glob module's docs, you'll see how it's implemented: https://docs.python.org/2/library/glob.html You may want to use fnmatch, or possibly just prepend a path name onto your glob. That way, you wouldn't need to change directory, which is almost always a bad idea for a library function. Instead of the reversed() call, you can simply add reverse=True to the sorted() call. And since you're not making use of the original list, you could use its sort() method (which has key and reverse parameters too) to sort a bit more efficiently. But I think your code is pretty good! ChrisA
Back to top | Article view | comp.lang.python
csiph-web