Ricard Le
Ricard Le

Reputation: 55

filtered list of files in a directory

I have files in the directory with the following naming convention arriving daily and wish to filter a list of file to interoperate the data. I have been playing with regular and finding it difficult match the file I want to filter.

import re

fnames = ["JEExport_20150501-20150531_Credit-Balance-Adjustment-Applied_20150531183249.csv",
        "JEExport_20150501-20150531_External-Credit-Balance-Payment_20150531183254.csv",
        "JEExport_20150501-20150531_External-Payment_20150531183251.csv",
        "JEExport_20150501-20150531_Invoice-Item_20150531183255.csv",
        "JEExport_20150501-20150531_Invoice-Item-Adjustment_20150531183304.csv"
        "JEExport_20150501-20150531_Invoice-Item-Adjustment-Tax_20150531183313.csv",
        "JEExport_20150501-20150531_Taxation-Item_20150531183240.csv"]

for element in fnames:    
    m = re.match("[A-Za-z]+\-[A-Za-z]+|-[A-Za-z]+", element)    
    if m == 'Invoice-Item-Adjustment':
        print(element) # do something

Upvotes: 1

Views: 40

Answers (1)

maxymoo
maxymoo

Reputation: 36545

I don't think you need regex. Does this work?

fnames = ["JEExport_20150501-20150531_Credit-Balance-Adjustment-Applied_20150531183249.csv",
        "JEExport_20150501-20150531_External-Credit-Balance-Payment_20150531183254.csv",
        "JEExport_20150501-20150531_External-Payment_20150531183251.csv",
        "JEExport_20150501-20150531_Invoice-Item_20150531183255.csv",
        "JEExport_20150501-20150531_Invoice-Item-Adjustment_20150531183304.csv",
        "JEExport_20150501-20150531_Invoice-Item-Adjustment-Tax_20150531183313.csv",
        "JEExport_20150501-20150531_Taxation-Item_20150531183240.csv"]

for element in fnames:        
    if 'Invoice-Item-Adjustment' in element:
        print(element) # do something

JEExport_20150501-20150531_Invoice-Item-Adjustment_20150531183304.csv
JEExport_20150501-20150531_Invoice-Item-Adjustment-Tax_20150531183313.csv

Upvotes: 1

Related Questions