2013-06-24 74 views
1

我想列出所有具有相同名稱的文件,而不考慮其擴展名。獲取具有相同名稱的文件,而不考慮其擴展名

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 
+0

'os.walk'不會做globbing – simonzack

回答

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()函數(WLL爲get_all_files() )在這個版本中是發電機,就像os.walk()

相關問題