pytool icon indicating copy to clipboard operation
pytool copied to clipboard

文件名匹配方法

Open limboinf opened this issue 9 years ago • 0 comments

如在某个路径下列出.py文件,可通过如下方法:

  1. 正则
  2. startswith& endswith
  3. glob 模块
  4. fnmatch模块

如下:

pyfiles = [name for name in os.listdir('somedir')
        if name.endswith('.py')]


import glob
pyfiles = glob.glob('somedir/*.py')

from fnmatch import fnmatch
pyfiles = [name for name in os.listdir('somedir')
            if fnmatch(name, '*.py')]

limboinf avatar Jan 13 '16 15:01 limboinf