3

If PurePath.match(pattern) does

Match this path against the provided glob-style pattern. Return True if matching is successful, False otherwise

and Path.glob(pattern) will

Glob the given pattern in the directory represented by this path, yielding all matching files (of any kind):

Question-1

Then why the below assertion fails?

query_files = Path('../').glob("sql-queries/**/*.sql")
filtered_files = [fname for fname in query_files if fname.match("sql-queries/**/*.sql")]
assert query_files == filtered_files # This assertion fails?

Question-2

How can we modify the PurePath.match pattern, to ensure the assertion does not fail.

NB The recursive pattern fails to Path.match the files under base path which are matched and returned by Path.glob. This seems to be the observable discrepancies between the two APIs.

QnA

Q: Can you give an example of a specific path that appears in query_files but not filtered_files?

A:

query_files = <class 'list'>: [
                    PosixPath('../sql-queries/combined_gv.sql'), 
                    PosixPath('../sql-queries/gv_with_merchant.sql'), 
                    PosixPath('../sql-queries/merchant_dimension.sql'), 
                    PosixPath('../sql-queries/gen_test_data/combined_gv.sql'),
                    PosixPath('../sql-queries/gen_test_data/merchant_dimension.sql')]
filtered_files = <class 'list'>: [
                    PosixPath('../sql-queries/gen_test_data/combined_gv.sql'),
                    PosixPath('../sql-queries/gen_test_data/merchant_dimension.sql')]
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • Can you give an example of a specific path that appears in `query_files` but not `filtered_files`? – user2357112 Sep 29 '19 at 08:35
  • @user2357112: See my edit. Have answered your comment with an example – Abhijit Sep 29 '19 at 08:41
  • @user2357112 Note that `Path.glob('*')` includes dot-prefixed paths, like `.hidden.txt`, unlike `glob.glob`. See also https://stackoverflow.com/questions/49862648/why-do-glob-glob-and-pathlib-path-glob-treat-hidden-files-differently and [Python issue 26096](https://bugs.python.org/issue26096) (which was deemed a feature by GvR); that may have to do with this. – 9769953 Sep 29 '19 at 08:56

1 Answers1

3

Path.glob and Path.match are backed by different glob implementations with different behavior. Particularly, Path.match doesn't support **.

It's weird and inconsistent and probably not originally intended, but I don't know if they'll change it.

user2357112
  • 260,549
  • 28
  • 431
  • 505