Coverage for pyguymer3/return_file_list.py: 55%

22 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-08 18:47 +0000

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def return_file_list( 

5 path, 

6 /, 

7 *, 

8 allowHidden = False, 

9 debug = __debug__, 

10 ensureNFC = True, 

11 follow_symlinks = True, 

12 return_symlinks = True, 

13): 

14 """Return a recursive list of file names in a directory. 

15 

16 This function returns a sorted list of file names recursively in a directory. 

17 

18 Parameters 

19 ---------- 

20 path : str 

21 the directory to search 

22 allowHidden : bool, optional 

23 allow hidden files 

24 debug : bool, optional 

25 print debug messages 

26 ensureNFC : bool, optional 

27 ensure that the Unicode encoding is NFC 

28 follow_symlinks : bool, optional 

29 follow symbolic links 

30 return_symlinks : bool, optional 

31 include symbolic links in the returned list 

32 

33 Returns 

34 ------- 

35 ans : list of str 

36 the sorted list of file names 

37 

38 Notes 

39 ----- 

40 Copyright 2017 Thomas Guymer [1]_ 

41 

42 References 

43 ---------- 

44 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3 

45 """ 

46 

47 # Import standard modules ... 

48 import os 

49 

50 # Import sub-functions ... 

51 from .make_path_safe import make_path_safe 

52 

53 # Create empty list ... 

54 contents = [] 

55 

56 # Check it exists ... 

57 if os.path.exists(path): 

58 # Open folder ... 

59 with os.scandir(path) as scanObj: 

60 # Loop over folder contents ... 

61 for entry in scanObj: 

62 # Check if the user wants to perform debugging ... 

63 if debug: 

64 # Test if this part is hidden and print the full path for 

65 # identification ... 

66 if not allowHidden and entry.name.startswith("."): 

67 print(f"WARNING: \"{entry.path}\" is hidden") 

68 

69 # Test if this part is illegal and print the full path for 

70 # identification ... 

71 if not entry.name.startswith(".") and entry.name != make_path_safe(entry.name, allowHidden = allowHidden, ensureNFC = ensureNFC): 

72 print(f"WARNING: \"{entry.path}\" is illegal") 

73 

74 # Check if it might need following ... 

75 if entry.is_dir(follow_symlinks = follow_symlinks): 

76 # Check that the directory is list-able ... 

77 # NOTE: On 20/Aug/2022 this was (incorrectly, in my opinion) 

78 # returning False on regular folders on FreeBSD (but 

79 # not MacOS) when passed "follow_symlinks = False". 

80 if os.access(entry, os.X_OK): 

81 # Recursively run this function again and add to the 

82 # list ... 

83 contents += return_file_list( 

84 entry.path, 

85 allowHidden = allowHidden, 

86 debug = debug, 

87 ensureNFC = ensureNFC, 

88 follow_symlinks = follow_symlinks, 

89 return_symlinks = return_symlinks, 

90 ) 

91 elif debug: 

92 print(f"WARNING: \"{entry.path}\" cannot be listed") 

93 

94 # Check if it might need adding to the list ... 

95 if entry.is_file(follow_symlinks = return_symlinks): 

96 # Add to the list ... 

97 contents.append(entry.path) 

98 elif debug: 

99 print(f"WARNING: \"{path}\" does not exist") 

100 

101 # Return sorted list ... 

102 return sorted(contents)