2

I'm a python newb so please be gentle..

I'm using glob to gather a list of files that match a specific pattern

for name in glob.glob('/home/myfiles/*_customer_records_2323_*.zip')
print '\t', name

The output is then something like

/home/myfiles/20130110_customer_records_2323_something.zip
/home/myfiles/20130102_customer_records_2323_something.zip
/home/myfiles/20130101_customer_records_2323_something.zip
/home/myfiles/20130103_customer_records_2323_something.zip
/home/myfiles/20130104_customer_records_2323_something.zip
/home/myfiles/20130105_customer_records_2323_something.zip
/home/myfiles/20130107_customer_records_2323_something.zip
/home/myfiles/20130106_customer_records_2323_something.zip

But I would like the output to be only the newest 5 files only (via the timestap or the os reported creation time)

/home/myfiles/20130106_customer_records_2323_something.zip
/home/myfiles/20130107_customer_records_2323_something.zip
/home/myfiles/20130108_customer_records_2323_something.zip
/home/myfiles/20130109_customer_records_2323_something.zip
/home/myfiles/20130110_customer_records_2323_something.zip

And Idea on how I can accomplish this? (have the list be sorted and then contain only the newest 5 files?)

UPDATE Modified to show how output from glob is NOT sorted by default

ReggieS2422
  • 57
  • 2
  • 6
  • 2
    See this answer to list files order by timestamps: http://stackoverflow.com/a/4500607/665869 – mitnk Feb 07 '13 at 04:44

2 Answers2

4

Use list-slicing:

for name in glob.glob('/home/myfiles/*_customer_records_2323_*.zip')[-5:]:
    print '\t', name

EDIT: if glob doesn't automatically sort the output, try the following:

for name in sorted(glob.glob('/home/myfiles/*_customer_records_2323_*.zip'))[-5:]:
    print '\t', name
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
  • thanks I'll test this out, I think it needs one more : after the close ] (I got an error without it) – ReggieS2422 Feb 07 '13 at 02:37
  • Oh, yeah, it does need a colon to open the `for` scope. – Kyle Strand Feb 07 '13 at 02:38
  • I just tested it and updated the question, glob does not sort output. Sorry for the mislead as my example showed – ReggieS2422 Feb 07 '13 at 02:41
  • 1
    mitnk's comment on your original question is a good point. It's probably much more reliable to sort by timestamps than alphabetically, which is what `sorted` does by default (though you can use the `key` parameter to sort however you want). – Kyle Strand Feb 07 '13 at 16:44
1

Slice the output:

glob.glob('/home/myfiles/*_customer_records_2323_*.zip')[-5:]

I'm not sure if glob's output is guaranteed to be sorted.

Blender
  • 289,723
  • 53
  • 439
  • 496