用Python拥有.gitignore样式fnmatch()的最简单方法是什么。看起来stdlib没有提供match()函数,该函数将针对UNIX样式路径正则表达式匹配路径规范。

  • fnmatch()仅匹配纯文件名,不匹配路径http://docs.python.org/library/fnmatch.html?highlight=fnmatch#fnmatch
  • glob()将执行目录列表,并且不提供match()true/false样式函数http://docs.python.org/py3k/library/glob.html?highlight=glob#glob.glob

  • .gitignore同时具有路径和带通配符的文件(黑名单)
  • https://github.com/miohtama/Krusovice/blob/master/.gitignore
  • http://linux.die.net/man/5/gitignore
  • 最佳答案

    如果您要使用.gitignore示例中列出的混合UNIX通配符模式,为什么不只采用每种模式并将fnmatch.translatere.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将通配符模式转换为re模式

    隐藏的UNIX文件:
    s = '/path/to/hidden/.file'
    isHiddenFile = re.search(fnmatch.translate('.*'), s)
    if not isHiddenFile:
        # do something with it
    

    10-01 11:56