[Easy question] - list 2 files in dir as old based on date


#1

Assume 6 files are stored in a dir named:
file_yyyMMdd.tar.gz
How do I print the 2 newest files as 'new' and rest marked as 'old' based on the date set in the name.

$  ./date_script.py
[old]      file_20120101.tar.gz
[old]      file_20130101.tar.gz
[old]      file_20140101.tar.gz
[old]      file_20141011.tar.gz
[new]    file_20151011.tar.gz
[new]    file_20161011.tar.gz
$

What modules and approach is required to list items as requested.

I got a start by listing the dirs.

import glob, os
os.chdir("/Users/resu/python/")
for file in glob.glob("*.tar.gz"):
    print(file)

#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.