Ad

POSIX-compliant Glob In Python

- 1 answer

I am implementing part of a C program in Python. This program uses glob(3) to find pathnames matching a pattern. This is a C function corresponding to the glob(7) shell builtin. They are specified in POSIX.2.

What is the equivalent of the POSIX glob(3) in Python?

I looked into the glob standard module but I am worried by the documentation: it does not mention POSIX anywhere, uses the fuzzy term "shell-like" and has some non-standard extensions such as wildstar ** patterns for recursive expansion.

Is it safe to use the glob.glob function if I do not use the recursive flag?

My requirements are:

  • Should match the results of glob(3), error handling can differ
  • Should work on both Python 2 and Python 3
  • Only Linux support is required
  • Answers mentioning PyPi packages are fine, I prefer smaller packages that can be audited and vendored.
  • I can use ctypes but I am looking for a Pythonic solution
Ad

Answer

Looking at how glob is implemented, it does not rely on glob(3) so it may, but is not guaranteed to yield identical results. Similar goes for POSIX compliance (apart form obviously extended functionality). There might be an overlap, but there is no guaranteed and it was apparently not the intention behind the module.

You can basically rephrase the question asking yourself. What features/behavior do I really need and does glob.glob() do what I specifically need? Chances are, it does.

Or am I rather looking for a compliance statement (certification even) because for instance the project/customer/our company (for reasons only they know (or do not)) requires it. For which you are out of luck with glob.glob(), unless you do the auditing/paperwork yourself or hire someone to do it (note, the code for glob is not that large). You can also (even though you said you did not want to) load glob(3) from libc.so with ctypes. Or re-implement it in Python from scratch (or based on glob for that matter) with the compatibility requirement in mind. As for the point on possible already existing alternatives, I'd defer that to further research and/or recommendations of others.

Ad
source: stackoverflow.com
Ad