Skip to content

Instantly share code, notes, and snippets.

@fredrikaverpil
Last active March 12, 2022 06:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredrikaverpil/9761434 to your computer and use it in GitHub Desktop.
Save fredrikaverpil/9761434 to your computer and use it in GitHub Desktop.
Walk folder and filter on filetype #python
import os, fnmatch
startDirectory = 'C:/temp'
filetypes = ['*.jpg', '*.exr']
for root, dirs, files in os.walk( startDirectory ):
for extension in ( tuple(filetypes) ):
for filename in fnmatch.filter(files, extension):
filepath = os.path.join(root, filename)
if os.path.isfile( filepath ):
# do something ...
@rahu6566
Copy link

rahu6566 commented Jan 24, 2021

`import os, fnmatch

startDirectory = '/'
filetypes = ['.jpg', '.exe']

for root, dirs, files in os.walk( startDirectory ):
for extension in ( tuple(filetypes) ):
for filename in fnmatch.filter(files, extension):
filepath = os.path.join(root, filename)
print(os.path.join( filepath ))
# do something ...`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment