Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting files by date

I found this directory checking code on the web and modified it a little, so it would print out added files. There's a buoy that sends me readings every now-and-then, but sometimes the connection is lost and instead of one file it sends multiple files. I need the program to sort them for me by date created. Is there a way to do this?

import os, time
path_to_watch = 'c://Users//seplema//Documents//arvuti'
before = dict([(f, None) for f in os.listdir (path_to_watch)])
while 1:
    after = dict([(f, None) for f in os.listdir (path_to_watch)])
    added = [f for f in after if not f in before]
    if before == after:
        1==1
    else:
        if len(added)==1:
            print added[0]
        else:
            for i in range (0,len(added)):
                print added[i]
    time.sleep(10)
    before = after
like image 663
nils Avatar asked Jul 20 '11 09:07

nils


People also ask

How do I organize my computer files by date?

To sort files, open the folder containing all the files you'd like to organize, right-click within the folder, select Sort by, and then select how you want to sort the files: by name, date, type, size, or tags. Then it's easier to organize computer files from a certain time range.

How do you sort files in chronological order?

Click the sort option in the top right of the Files area and select Date from the dropdown. Once you have Date selected, you will see an option to switch between descending and ascending order.

How do I Name files so they sort by date?

For dates, use YYYY-MM-DD (or YYYYMMDD, or YYMMDD, or YYMM). To ensure that files are sorted in proper chronological order, the most significant date and time components should appear first followed by the least significant components.


1 Answers

added.sort(key=lambda x: os.stat(os.path.join(path_to_watch, x)).st_mtime)

Will sort the added list by the last modified time of the files

Use st_ctime instaed of st_mtime for creation time on Windows (it doesn't mean that on other platforms).

like image 194
agf Avatar answered Sep 30 '22 01:09

agf