What behavior would you expect?

Chris Angelico rosuav at gmail.com
Fri Feb 20 00:20:32 EST 2015


On Fri, Feb 20, 2015 at 4:13 PM, Jason Friedman <jsf80238 at 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



More information about the Python-list mailing list