python import filenames that match search

Solutions on MaxInterview for python import filenames that match search by the best coders in the world

showing results for - "python import filenames that match search"
Claudia
24 Aug 2016
1# Basic syntax:
2glob.glob('/directory/to/search/search_pattern') 
3# Where the search_pattern follows the Unix path expansion rules
4
5# Example usage:
6# Say you have a directory containing the following files and you only
7# want to get the filenames ending in png:
8/directory/file.png
9/directory/file1.txt
10/directory/file2.txt
11/directory/file3.png
12
13glob.glob('/directory/*png') # Returns list of matching paths:
14--> ['/directory/file.png', '/directory/filea.png']
15
16# If you want just the filenames, you can easily parse with split:
17files = glob.glob('/directory/*png')
18filenames = [files[i].split('/')[-1] for i in range(len(files))]
19print(filenames)
20--> ['file.png', 'filea.png']
21
22# Note, the glob module only supports the * and the ? wildcards