[Tutor] Sorting specific files by modification date

Michael Langford mlangford.cs03 at gtalumni.org
Mon Nov 5 23:59:40 CET 2007

In psudocode:

#map the files to their time.
filesByTimes = {}
for each in filesInDirectory:

#find the largest time
times = filesByTimes.keys()

#retrieve the file that goes with it
latestFile = filesByTimes[times[-1]]


On 11/5/07, Fiyawerx <fiyawerx at gmail.com> wrote:
> I'm working on a small script that so far, using the xlrd module, (
> http://www.lexicon.net/sjmachin/xlrd.html) will parse all the files in a
> given directory for a xls file with a specific worksheet. This way, if the
> file names change, or people don't save the spreadsheet with the right name,
> my script will still be able to locate the correct files to use for it's
> data source out of multiple files / versions. So far what I have sort of
> goes like this :
> import os
> import xlrd
> data = {}
> #path may be set externally at some point
> data['path'] = 'mypath_to_program'
> os.chdir(data['path'])
> data['xls_files'] = [ file for file in os.listdir('./') if '.xls' in file
> ]
> first_files = [ file for file in data['xls_files'] if u'First Worksheet'
> in xlrd.open_workbook(file).sheet_names() ]
> data['first_file'] = ??
> second_files = [ file for file in data['xls_files'] if u'Second Worsheet'
> in xlrd.open_workbook(file).sheet_names() ]
> data['second_file'] = ??
> This is where I get stuck, I'm trying to figure out how, from the files
> that match, I can select the file with the most current time stamp and use
> that as my main data file.
> I know I can get the modification time with os.stat(file).st_mtime, but
> I'm not sure how I can sort my returns by this, to get just the most current
> version. Any help / thoughts would be appreciated. I'm going to be looking
> for other worksheets as well that might be in other xls's, for example
> 'Second Worksheet' also, but I was just trying to get the 'first_files'
> working first. Instead of opening them each time, should I construct some
> type of data that stores the file, it's worksheets, and its modification
> times for each file found, and then just parse that list? like maybe change
> my xls_files around to not just be a list of names?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071105/8cd2f5ae/attachment.htm 

More information about the Tutor mailing list