How to get a directory list sorted by date?

Vincent Vande Vyvre vincent.vande.vyvre at telenet.be
Sun May 15 09:46:17 EDT 2016


Le 15/05/2016 10:47, cl at isbd.net a écrit :
> 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.
>
Hi,

Seeing all the answers, I think there's a misunderstood here.

The date used to sort the picture is not the file date provided by 
os.stat but the shooting date written into the metadata of each picture.

Can the OP confirm that ?

So, if the name of the picture are unchanged why not just compare the 
file name ?

Vincent



More information about the Python-list mailing list