*列表.目录中的Xyz文件

  • 本文关键字:Xyz 文件 列表 python
  • 更新时间 :
  • 英文 :


在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.

可以使用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.

最新更新