Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import os 

2from pathlib import Path 

3from typing import List 

4 

5 

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))