Last active
October 30, 2017 20:29
-
-
Save GISmd/d94d67a7f4f6c93728dfc49e3e473db7 to your computer and use it in GitHub Desktop.
Should return a list of files that end in a specified extension.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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