.gitignore style fnmatch()
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Digital Sunset Looping
--
Chapters
00:00 .Gitignore Style Fnmatch()
00:36 Accepted Answer Score 9
01:02 Answer 2 Score 26
01:29 Thank you
--
Full question
https://stackoverflow.com/questions/1004...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #regex #filenames #gitignore #fnmatch
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Digital Sunset Looping
--
Chapters
00:00 .Gitignore Style Fnmatch()
00:36 Accepted Answer Score 9
01:02 Answer 2 Score 26
01:29 Thank you
--
Full question
https://stackoverflow.com/questions/1004...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #regex #filenames #gitignore #fnmatch
#avk47
ANSWER 1
Score 26
There's a library called pathspec which implements the full .gitignore specification, including things like **/*.py; the documentation describes how to handle Git pattern matching (you can also see code).
>>> import pathspec
>>> spec_src = '**/*.pyc'
>>> spec = pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, spec_src.splitlines())
>>> set(spec.match_files({"test.py", "test.pyc", "deeper/file.pyc", "even/deeper/file.pyc"}))
set(['test.pyc', 'even/deeper/file.pyc', 'deeper/file.pyc'])
>>> set(spec.match_tree("pathspec/"))
set(['__init__.pyc', 'gitignore.pyc', 'util.pyc', 'pattern.pyc', 'tests/__init__.pyc', 'tests/test_gitignore.pyc', 'compat.pyc', 'pathspec.pyc'])
ACCEPTED ANSWER
Score 9
If you want to use mixed UNIX wildcard patterns as listed in your .gitignore example, why not just take each pattern and use fnmatch.translate with re.search?
import fnmatch
import re
s = '/path/eggs/foo/bar'
pattern = "eggs/*"
re.search(fnmatch.translate(pattern), s)
# <_sre.SRE_Match object at 0x10049e988>
translate turns the wildcard pattern into a re pattern
Hidden UNIX files:
s = '/path/to/hidden/.file'
isHiddenFile = re.search(fnmatch.translate('.*'), s)
if not isHiddenFile:
# do something with it