Friday, December 26, 2014

Python Regex

Problem:
    To list all the files in a directory in format with: ama.xxxx or ama.xxxx.gz (x is digital) and print them in the chronological order.

import os
import re

def listdir_fullpath(d):
    return [os.path.join(d, f) for f in os.listdir(d)]

AMA_FILE_NAMING = ".*ama.\d{4}(.gz|)$"
amaFilePattern = dirName + "/" + AMA_FILE_NAMING
amaFiles = sorted( [f for f in listdir_fullpath(dirName) if re.match(AMA_FILE_NAMING, f)], key=os.path.getmtime)

for f in amaFiles:
    print f

@http://hilite.me/


For a simple match, glob can be used.

No comments: