在python中是否有一个内置函数,使用I can list all *。从目录中的Xyz文件?如果不是,那怎么可能管理它呢?
您可以使用glob
模块:
import glob
your_list = glob.glob('*.xyz')
help on glob.glob
:
>>> print glob.glob.__doc__
Return a list of paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la
fnmatch. However, unlike fnmatch, filenames starting with a
dot are special cases that are not matched by '*' and '?'
patterns.
可以使用glob
或os
(我更喜欢glob
)
from glob import glob
file_list = glob('*.xyz')
或
import os
file_list = [i for i in os.listdir(os.getcwd()) if i.endswith('.xyz')]
使用glob
允许您不自己过滤结果,但自定义过滤允许检查复杂规则
filenames starting with a dot are special cases that are not matched by '*' and '?' patterns.