@@ -774,6 +774,7 @@ def iterchildren(
774
774
self : _PP ,
775
775
exclude_dirs : Optional [Iterable [str ]] = unwanted_dirs ,
776
776
match : Optional [str ] = None ,
777
+ matchcase : bool = True ,
777
778
) -> Iterator [_PP ]:
778
779
"""
779
780
Returns an iterator over all children (files and directories) of the current path object.
@@ -784,6 +785,11 @@ def iterchildren(
784
785
together with their children.
785
786
:param match: A pattern to match filenames against.
786
787
The pattern should be in the format taken by :func:`~.matchglob`.
788
+ :param matchcase: Whether the filename's case should match the pattern.
789
+
790
+ :rtype:
791
+
792
+ .. versionchanged:: 2.5.0 Added the ``matchcase`` option.
787
793
"""
788
794
789
795
if not self .is_dir ():
@@ -801,12 +807,12 @@ def iterchildren(
801
807
if any (d in parts for d in exclude_dirs ):
802
808
continue
803
809
810
+ if match is None or (match is not None and matchglob (file , match , matchcase )):
811
+ yield file
812
+
804
813
if file .is_dir ():
805
814
yield from file .iterchildren (exclude_dirs , match )
806
815
807
- if match is None or (match is not None and matchglob (file , match )):
808
- yield file
809
-
810
816
811
817
class PosixPathPlus (PathPlus , pathlib .PurePosixPath ):
812
818
"""
@@ -878,7 +884,7 @@ def traverse_to_file(base_directory: _P, *filename: PathLike, height: int = -1)
878
884
raise FileNotFoundError (f"'{ filename [0 ]!s} ' not found in { base_directory } " )
879
885
880
886
881
- def matchglob (filename : PathLike , pattern : str ):
887
+ def matchglob (filename : PathLike , pattern : str , matchcase : bool = True ):
882
888
"""
883
889
Given a filename and a glob pattern, return whether the filename matches the glob.
884
890
@@ -888,12 +894,19 @@ def matchglob(filename: PathLike, pattern: str):
888
894
:param pattern: A pattern structured like a filesystem path, where each element consists of the glob syntax.
889
895
Each element is matched by :mod:`fnmatch`.
890
896
The special element ``**`` matches zero or more files or directories.
897
+ :param matchcase: Whether the filename's case should match the pattern.
898
+
899
+ :rtype:
891
900
892
901
.. seealso::
893
902
894
903
:wikipedia:`Glob (programming)#Syntax` on Wikipedia
904
+
905
+ .. versionchanged:: 2.5.0 Added the ``matchcase`` option.
895
906
"""
896
907
908
+ match_func = fnmatch .fnmatchcase if matchcase else fnmatch .fnmatch
909
+
897
910
filename = PathPlus (filename )
898
911
899
912
pattern_parts = deque (pathlib .PurePath (pattern ).parts )
@@ -927,16 +940,16 @@ def matchglob(filename: PathLike, pattern: str):
927
940
# Filename must match everything after **
928
941
return False
929
942
930
- if fnmatch . fnmatchcase (filename_part , pattern_part ):
943
+ if match_func (filename_part , pattern_part ):
931
944
continue
932
945
else :
933
- while not fnmatch . fnmatchcase (filename_part , pattern_part ):
946
+ while not match_func (filename_part , pattern_part ):
934
947
if not filename_parts :
935
948
return False
936
949
937
950
filename_part = filename_parts .popleft ()
938
951
939
- elif fnmatch . fnmatchcase (filename_part , pattern_part ):
952
+ elif match_func (filename_part , pattern_part ):
940
953
continue
941
954
else :
942
955
return False
0 commit comments