Skip to content

Instantly share code, notes, and snippets.

@pongo
Last active September 27, 2024 09:03
Show Gist options
  • Save pongo/19f84d9e1cf74ba04c912dae8e592dcc to your computer and use it in GitHub Desktop.
Save pongo/19f84d9e1cf74ba04c912dae8e592dcc to your computer and use it in GitHub Desktop.
Get all files in folder in python (recursive optional)
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)
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