Posts: 13
Threads: 8
Joined: Jan 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
Posts: 2,953
Threads: 48
Joined: Sep 2016
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']
Posts: 8,162
Threads: 160
Joined: Sep 2016
you can use os.path.splitext() or str method .endswith() to check filename ext
Posts: 2,953
Threads: 48
Joined: Sep 2016
You are asking to list all files in a directory tree?
Posts: 7,320
Threads: 123
Joined: Sep 2016
Jan-12-2017, 08:29 PM
(This post was last modified: Jan-12-2017, 08:32 PM by snippsat.)
(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)
Posts: 2,953
Threads: 48
Joined: Sep 2016
Jan-12-2017, 09:33 PM
(This post was last modified: Jan-12-2017, 09:33 PM by wavic.)
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)
Posts: 7,320
Threads: 123
Joined: Sep 2016
Jan-12-2017, 09:57 PM
(This post was last modified: Jan-12-2017, 10:02 PM by snippsat.)
(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() .
This will increase the speed of os.walk() very significantly (as mentioned above, by 2-20 times, depending on the system)
Look better with endswith() :
if os.path.splitext(file)[1] == '.mp3':
# Or
if file.endswith('.mp3')
Posts: 2,953
Threads: 48
Joined: Sep 2016
Jan-12-2017, 10:05 PM
(This post was last modified: Jan-12-2017, 10:05 PM by wavic.)
And they say that scandir() has significant speed improvement especially on Windows. Since 3.5
Posts: 7,320
Threads: 123
Joined: Sep 2016
Jan-12-2017, 10:23 PM
(This post was last modified: Jan-12-2017, 10:24 PM by snippsat.)
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.
|