pytool
pytool copied to clipboard
文件名匹配方法
如在某个路径下列出.py
文件,可通过如下方法:
- 正则
- startswith& endswith
- glob 模块
- 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')]