Python Forum
List files with a specific extension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List files with a specific extension
#1
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
Reply
#2
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']
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
you can use os.path.splitext() or str method .endswith() to check filename ext
Reply
#4
You are asking to list all files in a directory tree?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(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)
Reply
#6
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)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(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')
Reply
#8
And they say that scandir() has significant speed improvement especially on Windows. Since 3.5
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
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 Cool
Quote:about 7-50 times as fast on Windows, and about 3-10 times as fast on Linux and Mac OS X.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list the files using query in python arjunaram 0 652 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  python print all files which contain specific word in it mg24 5 1,188 Jan-27-2023, 11:20 AM
Last Post: snippsat
  python move specific files from source to destination including duplicates mg24 3 1,051 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  search a list or tuple for a specific type ot class Skaperen 8 1,854 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  How to download a list of files from FTP? schnarkle 0 974 Jun-21-2022, 10:35 PM
Last Post: schnarkle
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,475 Apr-04-2022, 09:29 PM
Last Post: Larz60+
Question How to gather specific second-level items from a list chatguy 2 1,517 Dec-17-2021, 05:05 PM
Last Post: chatguy
  get all the files in the path in a list? korenron 23 6,858 Jul-19-2021, 07:44 AM
Last Post: korenron
  Moving specific files then unzipping/decompressing christophereccles 2 2,326 Apr-24-2021, 04:25 AM
Last Post: ndc85430
  Searching for specific word in text files. JellyCreeper6 1 1,698 Nov-03-2020, 01:52 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020