Last active
September 27, 2024 09:03
-
-
Save pongo/19f84d9e1cf74ba04c912dae8e592dcc to your computer and use it in GitHub Desktop.
Get all files in folder in python (recursive optional)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Iterator | |
from pathlib import Path | |
def get_all_files(root: Path | str, recursive=False) -> Iterator[Path]: | |
for item in Path(root).iterdir(): | |
if item.is_file(): | |
yield item | |
elif recursive and item.is_dir(): | |
yield from get_all_files(item, recursive=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def print_files_in_folder(root: str): | |
for file in get_all_files(root): | |
print(file) | |
def get_videos_recursive(root: str) -> Iterator[Path]: | |
return (v for v in get_all_files(root, recursive=True) if v.suffix.lower() == '.mp4') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment