From: Tim Chase on
On 07/13/2010 10:56 AM, python(a)bdurham.com wrote:
> Any recommendations for a cross-platform module that creates a
> directory object with not only file names, but file attributes as
> well?
>
> Use cases:
> - Sort files by file size or date last modified
> - Filter files by read-only status
>
> I know I can use various standard library functions [1] to
> construct such a class, but I'd rather avoid re-inventing the
> wheel if at all possible. (I fear that this may be one of those
> seemingly easy tasks that starts off simple and ends up taking a
> lot more time to implement after real world refactoring).

I think it's sufficiently easy to create a custom solution that
there's not much value in trying to map file-metadata into a
wrapper object. For your examples:

f = os.path.getmtime
#f = os.path.getsize
sorted_by_mtime = sorted(os.listdir('.'), key=f)

or if you need a different location

sorted_by_mtime = sorted(
os.listdir(loc),
key=lambda fname: f(os.path.join(loc, fname))
)

And for your read-only status:

files = [fname for fname in os.listdir(loc)
if os.access(os.path.join(loc, fname), os.W_OK)
]

That doesn't mean somebody hasn't done it before, but given how
easy it is without a wrapper object, it's not something I'd go
out of my way to find.

-tkc