hiya guys n girls,

i am making a prgram where it sorts a list of files and pulls the 6 most recent files..

that is fine but i need it to pull 6 different dates ie...

(this is what it does now
test1 - 24/06/10
test2 - 24/06/10
test3 - 24/06/10
test4 - 28/06/10
test5 - 28/06/10
test6 - 29/06/10
)
(this is what i want it to do
test1 - 19/07/10
test2 - 14/07/10
test3 - 09/07/10
test4 - 02/07/10
test5 - 30/06/10
test6 - 29/06/10
)
i have this code which pulls the last 6 modifed dates if this helps

for lastmod_seconds, path in date_file_list[:6]:
    folder, file_name = os.path.split(path)
    # convert seconds to time tuple
    lastmod_tuple = time.localtime(lastmod_seconds)
    # convert time tuple to MM/DD/YYYY HH:MM:SS format
    file_date = time.strftime("%m/%d/%y %H:%M:%S", lastmod_tuple)
    print "%-40s %s" % (file_name, file_date)

Recommended Answers

All 2 Replies

If I understand the question, you want to print file dates that have not already been printed so you have to use some sort of container, a list for example, which contains the dates already printed, and print only if the date has not already been used.

hi there yes thats correct i need to collect the 6 latest backup files but they have to be different dates i have got to the point where i canget the 6 latest dates but most of them are the same.

# sorts files by modifed date,
# pulls 6 most recent files,
# and delete all others.
#created by dan holding v0.1 (03 Aug 2010)
import os, glob, time, sets
root = '.'
date_file_list = []
date_file_set = []
for folder in glob.glob(root):
    print "folder =", folder
    for file in glob.glob(folder + '\*.txt*'):
# /\ select the type of file, for instance *.bat or all files *.*
        stats = os.stat(file)
        lastmod_seconds = stats[8]
        # /\ use seconds first to sort the time
        date_file_tuple = lastmod_seconds, file
        date_file_list.append(date_file_tuple)
        # \/ create a set to eliminate duplicate files
        # with matching seconds and path/name
        date_file_set = set(date_file_list)
        # \/ convert set back to a list
        date_file_list = list(date_file_set)
        # \/ latest modified date (in seconds) now first
        date_file_list.sort(reverse = True)
#\/ test
print (len(date_file_list))
print '/\ number of files in list '*1,'-'*50
print (len(date_file_set))
print '/\ number of files in set'*1,'-'*52
# show six most recent files
for lastmod_seconds, path in date_file_list[:6]:
    folder, file_name = os.path.split(path)
    # convert seconds to time tuple
    lastmod_tuple = time.localtime(lastmod_seconds)
    # convert time tuple to MM/DD/YYYY HH:MM:SS format
    file_date = time.strftime("%m/%d/%y %H:%M:%S", lastmod_tuple)
    print "%-40s %s" % (file_name, file_date)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.