Coverage for pyguymer3/return_file_list.py: 50%

26 statements  

« prev     ^ index     » next       coverage.py v7.10.3, created at 2025-08-16 08:31 +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_dsstore = True, 

13 return_symlinks = True, 

14): 

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

16 

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

18 

19 Parameters 

20 ---------- 

21 path : str 

22 the directory to search 

23 allowHidden : bool, optional 

24 allow hidden files 

25 debug : bool, optional 

26 print debug messages 

27 ensureNFC : bool, optional 

28 ensure that the Unicode encoding is NFC 

29 follow_symlinks : bool, optional 

30 follow symbolic links 

31 return_dsstore : bool, optional 

32 include ".DS_Store" files in the returned list 

33 return_symlinks : bool, optional 

34 include symbolic links in the returned list 

35 

36 Returns 

37 ------- 

38 ans : list of str 

39 the sorted list of file names 

40 

41 Notes 

42 ----- 

43 Copyright 2017 Thomas Guymer [1]_ 

44 

45 References 

46 ---------- 

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

48 """ 

49 

50 # Import standard modules ... 

51 import os 

52 

53 # Import sub-functions ... 

54 from .make_path_safe import make_path_safe 

55 

56 # Create empty list ... 

57 contents = [] 

58 

59 # Check it exists ... 

60 if os.path.exists(path): 

61 # Open folder ... 

62 with os.scandir(path) as scanObj: 

63 # Loop over folder contents ... 

64 for entry in scanObj: 

65 # Test if this part is a ".DS_Store" file and skip ... 

66 if not return_dsstore and entry.name == ".DS_Store": 

67 if debug: 

68 print(f"DEBUG: \"{entry.path}\" is a \".DS_Store\" file") 

69 continue 

70 

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

72 if debug: 

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

74 # identification ... 

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

76 print(f"DEBUG: \"{entry.path}\" is hidden") 

77 

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

79 # identification ... 

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

81 print(f"DEBUG: \"{entry.path}\" is illegal") 

82 

83 # Check if it might need following ... 

84 if entry.is_dir(follow_symlinks = follow_symlinks): 

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

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

87 # returning False on regular folders on FreeBSD (but 

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

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

90 # Recursively run this function again and add to the 

91 # list ... 

92 contents += return_file_list( 

93 entry.path, 

94 allowHidden = allowHidden, 

95 debug = debug, 

96 ensureNFC = ensureNFC, 

97 follow_symlinks = follow_symlinks, 

98 return_dsstore = return_dsstore, 

99 return_symlinks = return_symlinks, 

100 ) 

101 elif debug: 

102 print(f"DEBUG: \"{entry.path}\" cannot be listed") 

103 

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

105 if entry.is_file(follow_symlinks = return_symlinks): 

106 # Add to the list ... 

107 contents.append(entry.path) 

108 elif debug: 

109 print(f"DEBUG: \"{path}\" does not exist") 

110 

111 # Return sorted list ... 

112 return sorted(contents)