python中是否有内置函数,使用我可以列出目录中的所有* .xyz文件? 如果没有,那么如何管理呢?
答案 0 :(得分:5)
您可以使用glob
模块:
import glob
your_list = glob.glob('*.xyz')
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.
答案 1 :(得分:0)
可以使用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.