Yes, that was the point I was making.
I think the point here is that we’re talking about an application that accepts user input of a “file pattern” and wants to turn that into a list of files. Traditionally, this is done with glob.glob
, which takes a string argument representing a pattern to match against the filesystem.
The pathlib.Path.glob
method is different - it takes a string as a pattern in a similar way, but the pattern is required to be relative, and is matched against the contents of the path object in self
.
The OP’s use case is the first situation, but they want the ability to match against non-filesystem patterns, which glob
doesn’t support. The extensibility of pathlib is attractive here, but only if we can construct a suitable base pathlib subclass. The problem is that if we follow the logic of glob.glob
, the pattern is general, and in theory the user could enter something that has a wildcard in the URI scheme part of the path. Clearly that makes no sense, and in particular it doesn’t allow us to know which pathlib subclass would apply. So in practice a fully general pattern string isn’t valid.
If we make the restriction that the “root” (however we choose to define that) of the pattern must be fixed (no wildcards) then it’s easy to implement the relevant search:
root, pattern = split_user_pattern(input_string)
list_of_paths = Path(root).glob(pattern)
Implementing split_user_pattern
is left as an exercise for the reader. It depends heavily on the application and valid path provider types, but something like
def split_user_pattern(input_string):
path = Path(input_string)
return path.parts[0], os.fspath(Path(path.parts[1:]))
isn’t an implausible implementation in the absence of any application-specific constraints.
This is why I don’t see the one-line version of this as “hacky and unnatural”. The need to split and rejoin using Path.parts
is annoying - a Path.without_anchor
property would help a lot here - but that’s a very minor implementation detail, easily hidden in a helper function (as I did here).