How to get a directory list sorted by date?

cl at isbd.net cl at isbd.net
Sun May 15 04:47:59 EDT 2016


I have a little Python program I wrote myself which copies images from
a camera (well, any mounted directory) to my picture archive.  The
picture archive is simply a directory hierarchy of dates with years at
the top, then months, then days.

My Python program simply extracts the date from the image (put there
by the camera) and copies the image to the appropriate place in the
picture archive.

There is one small bit of speeding up done by my program, it checks if
the file is already in the archive and doesn't copy if it's already
there.  I don't generally clear pictures off the camera memory cards
so it's often the case that most of the pcitures are already in the
archive and all I actually want to do is copy the last couple of weeks
of new pictures.

As camera memory card sizes get bigger (and images get more detailed)
this is beginning to take rather a long time.

So, to the question, how can I get a list of the files on the memory
card (i.e. files in a directory) in date/time order, latest first.  I
can then copy them until the first one I find which is already in the
archive.  The file date may not *exactly* match the date/time in the
image file but it will produce the right order which is what I need.

What I want is a list in the order produced by:-
    ls --sort=time

I suppose I could use a system call but that seems a little inelegant.

-- 
Chris Green
·



More information about the Python-list mailing list