-2

I have a file extension, I got it in this way:

fileName, fileExtension = os.path.splitext(abspath)

now I need my file extension to became a pattern, something like '*.fileExtension', included the quote characters. I should use it in a fnmatch like this:

if fnmatch.fnmatch(name, pattern)

any idea?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Antonio Falcone
  • 181
  • 3
  • 17

1 Answers1

1

Use string concatenation:

pattern = '*' + fileExtension

or use string formatting:

pattern = '*{}'.format(fileExtension)

or

pattern = '*%s' % fileExtension
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343