List files with a specific extension - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: List files with a specific extension (/thread-1565.html) |
List files with a specific extension - pyth0nus3r - Jan-12-2017 Hello I wanted to know if it is possible to list a particular type of file (let's say .mp4) from a directory recursively. I can list all files recursively within a particular directory but I don't how to search for files with specific extension. Thanks RE: List files with a specific extension - wavic - Jan-12-2017 Hello! In [5]: cd /media/storage/Audio/Music/ /media/storage/Audio/Music In [6]: import glob In [7]: glob.glob('*.mp3')[:10] Out[7]: ['Claude VonStroke-Aundy (DJ Marky & S.P.Y. Remix) (www.myfreemp3.cc).mp3', 'CMA - The Small Losses (Original Mix) (Chillstep) 16.09.2012.mp3', 'CMA - The Small Losses.mp3', 'Coca & Villa - La Noche.mp3', 'Credit - Hearts & Minds.mp3', 'Credit-Hearts And Minds.mp3', 'Crush.mp3', 'Cuppy Cake Song.mp3', 'Daddy Yankee_Pitbull_Lil Jon_Noreaga - GASOLINA w_JON.mp3', 'Dark0 - Last Transmission (original mix).mp3'] RE: List files with a specific extension - buran - Jan-12-2017 you can use os.path.splitext() or str method .endswith() to check filename ext RE: List files with a specific extension - wavic - Jan-12-2017 You are asking to list all files in a directory tree? RE: List files with a specific extension - snippsat - Jan-12-2017 (Jan-12-2017, 06:57 PM)pyth0nus3r Wrote: I can list all files recursively within a particular directory but I don't how to search for files with specific extension.You can use os.walk() or better os.scandir()(a lot faster) for Python 3.5--> These do recursively scan whole folder. Eg. import os source = 'path' for root,dirs,files in os.scandir(source): for file_name in files: if file_name.endswith(('.jpg', '.jpeg', '.mp4')): print(file_name) RE: List files with a specific extension - wavic - Jan-12-2017 scandir(path) do not scan the sub-directories.for root, dirs, files in os.walk(path): if files: for file in files: if os.path.splitext(file)[1] == '.mp3': print(file) RE: List files with a specific extension - snippsat - Jan-12-2017 (Jan-12-2017, 09:33 PM)wavic Wrote: scandir(path) do not scan the sub-directories.Okay thanks for info,look like they build scandir() into os.walk(). I did try it before it went into standard library, from GitHub and then it did work like os.walk(). os.walk() Quote:As part of this proposal, os.walk() will also be modified to use scandir() rather than listdir() and os.path.isdir() . Look better with endswith() :if os.path.splitext(file)[1] == '.mp3': # Or if file.endswith('.mp3') RE: List files with a specific extension - wavic - Jan-12-2017 And they say that scandir() has significant speed improvement especially on Windows. Since 3.5 RE: List files with a specific extension - snippsat - Jan-12-2017 Yes the speed is better,i did make this in 2014 Picture/Movie-Collector. So here use scandir before it went into standard library. I see in my code that is was scandir.walk(path) ,which now is build into os.walk() .Speed Quote:about 7-50 times as fast on Windows, and about 3-10 times as fast on Linux and Mac OS X. |