获取具有相同名称的文件,无论其扩展名如何

时间:2013-06-24 18:54:32

标签: python file-io

我想列出所有具有相同名称的文件,无论其扩展名如何。

当我尝试搜索没有扩展名的文件名但是当我提到带扩展名的文件名时,

os.walk会产生空列表。它列出了任何目录中存在的具有相同名称和扩展名的所有文件。

def get_all_files(path):
    Datafiles=[]

    for root,dirs,files in os.walk(path):
        for file in files:
            pathname=os.path.join(root,file)
            Datafiles.append([file,pathname])

    return Datafiles

1 个答案:

答案 0 :(得分:2)

您可以使用fnmatch.filter()功能识别感兴趣的文件名:

import os, fnmatch

def get_all_files(path, pattern):
    datafiles = []

    for root,dirs,files in os.walk(path):
        for file in fnmatch.filter(files, pattern):
            pathname = os.path.join(root, file)
            filesize = os.stat(pathname).st_size
            datafiles.append([file, pathname, filesize])

    return datafiles

print get_all_files('.', 'something.*') # all files named 'something'

但是,请注意,通过更多代码行,还可以创建支持所有os.walk()关键字参数的更通用的东西:

import os, fnmatch

def glob_walk(top, pattern, **kwargs):
    """ Wrapper for os.walk() that filters the files returned
        with a pattern composed of Unix shell-style wildcards
        as documented in the fnmatch module.
    """
    for root, dirs, files in os.walk(top, **kwargs):
        yield root, dirs, fnmatch.filter(files, pattern)

# sample usage
def get_all_files(path, pattern):
    for root, dirs, files in glob_walk(path, pattern):
        for file in files:
            pathname = os.path.join(root, file)
            filesize = os.stat(pathname).st_size
            yield file, pathname, filesize

print list(get_all_files('.', 'something.*')) # all files named 'something'

请注意,此版本中新的glob_walk()函数(如get_all_files()所示)是生成器,就像os.walk()一样。