1

python 中是否有内置函数,使用我可以列出目录中的所有 *.xyz 文件?如果没有,那怎么可能管理它?

4

2 回答 2

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.
于 2013-06-13T06:29:58.587 回答
0

一个可以使用globos(我想要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.

于 2013-06-13T06:58:10.320 回答