Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85946
| References | <CANy1k1g+wU6VUifZpV5+DWendT2LqS-PhRA-KC5hmq2xQ_uy+A@mail.gmail.com> <CANy1k1gMgGnqNcgTwjcqfYH-QctZthv89uXcU8SV9Uk6Wji=4A@mail.gmail.com> |
|---|---|
| Date | 2015-02-20 16:20 +1100 |
| Subject | Re: What behavior would you expect? |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18909.1424409635.18130.python-list@python.org> (permalink) |
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 comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: What behavior would you expect? Chris Angelico <rosuav@gmail.com> - 2015-02-20 16:20 +1100
csiph-web