Skip to content

Instantly share code, notes, and snippets.

@mhrstmnn
Last active April 13, 2024 10:45
Show Gist options
  • Save mhrstmnn/ee959f16f378aafdb97e0cb5cf7a164a to your computer and use it in GitHub Desktop.
Save mhrstmnn/ee959f16f378aafdb97e0cb5cf7a164a to your computer and use it in GitHub Desktop.
A simple script to find TOML files recursively
#!/usr/bin/env python3
import os
import sys
def find_toml_files() -> list[str]:
def find_toml_files_recursively(directory_path: str, found_toml_files: list[str]) -> None:
def get_directory_entries(directory_path: str) -> list[str]:
return list(map(lambda entry: os.path.join(directory_path, entry),
filter(lambda entry: not entry.startswith('.'), os.listdir(directory_path))))
def get_toml_files(entries: list[str]) -> list[str]:
return list(filter(lambda entry: not os.path.isdir(entry) and entry.endswith('.toml'), entries))
def get_directories(entries: list[str]) -> list[str]:
return list(filter(lambda entry: os.path.isdir(entry), entries))
directory_entries: list[str] = get_directory_entries(directory_path)
found_toml_files += get_toml_files(directory_entries)
for directory in get_directories(directory_entries):
find_toml_files_recursively(directory, found_toml_files)
found_toml_files: list[str] = []
if len(sys.argv) > 1:
try:
find_toml_files_recursively(' '.join(sys.argv[1:]), found_toml_files)
except FileNotFoundError as error:
print(error, file=sys.stderr)
sys.exit(1)
else:
find_toml_files_recursively('.', found_toml_files)
return found_toml_files
def main() -> int:
found_toml_files: list[str] = find_toml_files()
print(found_toml_files)
return 0
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment