How to make a chronological list of files with file modification date

I want to create a list of these files in a directory with the extension .bas and which were changed after June 1, 2011. In addition, I would like to know how it is not necessary to have files in the list with the date and time the file was last modified, for example, 'filename.bas 2011-06-04_AM.00.30'

I am new to python. I was able to view .bas files sorted by date using the following code:

from stat import S_ISREG, ST_CTIME, ST_MODE
import os, sys, time, glob
search_dir = r"e:\test"
files = filter(os.path.isfile, glob.glob(search_dir + "\\*.bas"))
files.sort(key=lambda x: os.path.getmtime(x))

But I can’t figure out how to add a “date” to the file name in the list.

Any suggestions would be much appreciated.

+5
source share
1 answer

date. , :

from stat import S_ISREG, ST_CTIME, ST_MODE
import os, sys, time, glob
search_dir = r"e:\test"
files = filter(os.path.isfile, glob.glob(search_dir + "\\*.bas"))
file_date_tuple_list = []
for x in files:
    d = os.path.getmtime(x)
    #tuple with file and date, add it in a list
    file_date_tuple = (x,d)
    file_date_tuple_list.append(file_date_tuple)
#sort the tuple list by the second element which is the date
file_date_tuple_list.sort(key=lambda x: x[1])

, ...

file_date_tuple_list = [(x,os.path.getmtime(x)) for x in files]
file_date_tuple_list.sort(key=lambda x: x[1])

for .

, , , ... ...

from datetime import date

, .

file_date_string_list = ["%s %s"%(x[0],date.fromtimestamp(x[1])) \
                                             for x in file_date_tuple_list]

reverse sort:

file_date_tuple_list.sort(key=lambda x: x[1],reverse=True)

datetime

from datetime import datetime
limit = datetime(2011,01,05,17,0,0) #5pm , Jun 5 2011 
file_date_string_list = ["%s %s"%(x[0],date.fromtimestamp(x[1])) \
                                       for x in file_date_tuple_list \
                                       if datetime.fromtimestamp(x[1]) > limit ]

, if , .

+4
source

All Articles