1

Trying to match files on disk that either end with .asm,ASM, or with some 1/2/3 digit extension like - .asm.1/.asm.11

My python code is-

asmFiles = glob.glob('*.asm') + glob.glob('*.ASM') + glob.glob('*.asm.[0-9]') + glob.glob('*.ASM.[0-9]')

How do I match the file '.asm.11' as my code can only match the first three?

Thanks

user3073180
  • 105
  • 1
  • 13

1 Answers1

0

Here is a solution using Python regex and list comprehension:

import re

files = ['foobar.asm', 'foobar.ASM', 'foobar.asm.1', 'foobar.ASM.11', 'foobarasm.csv']

asm_pattern = '\.(asm|ASM)$|(asm|ASM)\.[1-9]$|\.(asm|ASM)\.[1-9][1-9]$'

asmFiles = [f for f in files if re.search(asm_pattern, f)]

[print(asmFile) for asmFile in asmFiles]

The last element from list files is an edge case I thought about to test the search pattern. It does not appear in the result, as expected.