Python Forum
Put all files in a directory into list. How?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Put all files in a directory into list. How?
#1
Hello

I want to put all files recursively in a directory into a list with only certain type of extensions.

Here is what I did.

#!/usr/bin/env python3                                                           
                                                                                    
from glob import glob                                                               
                                                                                 
EXTN = ('*.avi', '*.flv', '*.mkv', '*.mov', '*.mp4',                             
        '*.mpeg', '*.mpg', '*.webm', '*.wmv')                                    
                                                                                 
video_files = []                                                                 
                                                                                 
for file in EXTN:                                                                
    video_files.extend(glob("**/*" + file, recursive = True))                    
                                                                                 
video_files.sort()
I'm using Python 3.8.5. Is this the proper way? Or there any best method I can use to store all files with a particular type of extensions in a list?

Thanks
Reply
#2
You can't pass a list of extensions to glob directly. So your choices are to run multiple globs and combine the results, or to return all the files and then throw out anything that doesn't match one of your extensions. For short trees where running the glob doesn't take long, the first one isn't bad. But if you have billions of files, it would be very inefficient.
Reply
#3
this can be done with pathlib:

I use same path as script file, but you can substitute any path on line 23
note that I removed the '*' from EXTN pathlib wants just the suffix.
I put two files in my src directory, cows.mpeg and trigger.mp4
import pathlib
import os

# This does all the work:
def get_file_list(fpath):
    EXTN = ['.avi', '.flv', '.mkv', '.mov', '.mp4',
        '.mpeg', '.mpg', '.webm', '.wmv']

    if not isinstance(fpath, pathlib.PosixPath):
        fpath = (pathlib.Path(fpath))

    return [filename for filename in fpath.iterdir()
        if filename.is_file() and (filename.suffix in EXTN)]


def main():
    filelist = []

    # anchor starting directory to script path
    os.chdir(os.path.abspath(os.path.dirname(__file__)))

    #create a pathlib path, here it is current path
    fpath = pathlib.Path('.')

    filelist = get_file_list(fpath)

    for filename in filelist:
        print(f"{filename.name}")


if __name__ == '__main__':
    main()
results:
Output:
cows.mpeg trigger.mp4
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 449 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  change directory of save of python files akbarza 3 876 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,307 Jun-27-2023, 01:17 PM
Last Post: diver999
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 1,627 May-07-2023, 12:33 PM
Last Post: deanhystad
  list the files using query in python arjunaram 0 670 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  Read directory listing of files and parse out the highest number? cubangt 5 2,338 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  How to download a list of files from FTP? schnarkle 0 1,006 Jun-21-2022, 10:35 PM
Last Post: schnarkle
  How to save files in a separate directory Scordomaniac 3 1,857 Mar-16-2022, 10:17 AM
Last Post: Gribouillis
  get all the files in the path in a list? korenron 23 7,020 Jul-19-2021, 07:44 AM
Last Post: korenron
  Rename Multiple files in directory to remove special characters nyawadasi 9 6,364 Feb-16-2021, 09:49 PM
Last Post: BashBedlam

Forum Jump:

User Panel Messages

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