-2

i want to make a list of filenames or filepathes with same extension that i set. For example, I have a directory C:\data with many files in it. I set extension parameter (for example .txt). What i need is to create a list of filepathes to those files, like this:

['C:\\data\\example1.txt','C:\\data\\example2.txt','C:\\data\\example3.txt']
Ivan Pudyakov
  • 116
  • 1
  • 1
  • 7
  • possible duplicate of [How to list all files of a directory in Python](http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python) – Niek de Klein Aug 11 '15 at 07:48

1 Answers1

2

You'll want to use the glob module:

>>> import glob
>>> glob.glob('C:\\data\\*.txt')
['C:\\data\\1.txt', 'C:\\data\\card.txt']

For more information, see the official documentation.

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62