Coverage for jbank/files.py: 87%
15 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-27 13:36 +0700
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-27 13:36 +0700
1import os
2from pathlib import Path
3from typing import List
6def list_dir_files(path: str, suffix: str = "") -> List[str]:
7 """
8 Lists all files (and only files) in a directory, or return [path] if path is a file itself.
9 :param path: Directory or a file
10 :param suffix: Optional suffix to match (case insensitive). Default is none.
11 :return: list of absolute paths to files
12 """
13 if suffix:
14 suffix = suffix.lower()
15 if Path(path).is_file():
16 files = [os.path.abspath(path)]
17 else:
18 files = []
19 for f in os.listdir(path):
20 file_path = os.path.join(path, f)
21 if Path(file_path).is_file():
22 if not suffix or f.lower().endswith(suffix):
23 files.append(os.path.abspath(file_path))
24 return list(sorted(files))