Skip to content

Instantly share code, notes, and snippets.

@GISmd
Last active October 30, 2017 20:29
Show Gist options
  • Save GISmd/d94d67a7f4f6c93728dfc49e3e473db7 to your computer and use it in GitHub Desktop.
Save GISmd/d94d67a7f4f6c93728dfc49e3e473db7 to your computer and use it in GitHub Desktop.
Should return a list of files that end in a specified extension.
def find_file_by_type(file_extension, directory=''):
if directory == '':
root = Tkinter.Tk()
directory = tkFileDialog.askdirectory(parent = root, initialdir = "/", title = 'Please Select a Directory')
root.withdraw()
os.chdir(directory)
targetfiles = []
for root, dirs, filenames in os.walk(directory):
for name in filenames:
if os.path.splitext(name)[1] == file_extension:
targetfiles.append(os.path.join(root,name))
if len(targetfiles) > 0:
print('{} {} files found'.format(str(len(targetfiles)),file_extension))
return targetfiles
else:
print('No {} file found'.format(file_extension))
return targetfiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment