Skip to content

Instantly share code, notes, and snippets.

@achavez
Last active August 29, 2015 14:07
Show Gist options
  • Save achavez/b1c6560f8af7267c76c3 to your computer and use it in GitHub Desktop.
Save achavez/b1c6560f8af7267c76c3 to your computer and use it in GitHub Desktop.
Get all files in a directory with a specific file extension
import os
def list_files(source, extension):
matches = []
for root, dirnames, filenames in os.walk(source):
for filename in filenames:
if filename.endswith((extension)):
print filename
# Ex: Get all files in the directory docs with the extension PDF
# Can be easily written to a text file by running at the command line: python file_list.py > file_list.txt
list_files('docs', '.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment