1

I want to list down all the files with same name irrespective of their extensions.

os.walk results in empty list when I try to search for the file name without extension but when i mention the file name with extension. It list down all the files present in any directory with that same name and extension.

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
4

1 回答 1

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().

于 2013-06-24T19:16:12.583 回答