pythonpath

pathlib.Path().glob() and multiple file extension


I need to specify multiple file extensions like pathlib.Path(temp_folder).glob('*.xls', '*.txt'):

How I can do it?

https://docs.python.org/dev/library/pathlib.html#pathlib.Path.glob


Solution

  • If you need to use pathlib.Path.glob()

    from pathlib import Path
    def get_files(extensions):
        all_files = []
        for ext in extensions:
            all_files.extend(Path('.').glob(ext))
        return all_files
    
    files = get_files(('*.txt', '*.py', '*.cfg'))