POSIX-compliant Glob In Python
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
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.
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module