Issue
I'm trying to loop through a folder and all subfolders to find all files of certain file types - for example, only .mp4, .avi, .wmv.
Here is what I have now, it loops through all file types:
import os
rootdir = 'input'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
print (os.path.join(subdir, file))
Solution
You can use os.path.splitext
which takes a path and splits the file extension from the end of it:
import os
rootdir = 'input'
extensions = ('.mp4', '.avi', '.wmv')
for subdir, dirs, files in os.walk(rootdir):
for file in files:
ext = os.path.splitext(file)[-1].lower()
if ext in extensions:
print (os.path.join(subdir, file))
Answered By - Ozgur Vatansever
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.