2009-11-12 70 views
60

我有一個文件可能位於每個用戶機器的不同位置。有沒有一種方法來實現文件的搜索?我可以通過文件名和目錄樹進行搜索的方法?在python中查找文件

回答

146

os.walk就是答案,這將找到的第一個匹配:

import os 

def find(name, path): 
    for root, dirs, files in os.walk(path): 
     if name in files: 
      return os.path.join(root, name) 

而這會發現所有的比賽:

def find_all(name, path): 
    result = [] 
    for root, dirs, files in os.walk(path): 
     if name in files: 
      result.append(os.path.join(root, name)) 
    return result 

這將匹配模式:

import os, fnmatch 
def find(pattern, path): 
    result = [] 
    for root, dirs, files in os.walk(path): 
     for name in files: 
      if fnmatch.fnmatch(name, pattern): 
       result.append(os.path.join(root, name)) 
    return result 

find('*.txt', '/path/to/dir') 
+1

注意,這些例子只能找到文件,而不是具有相同名稱的目錄。如果你想在這個名字的目錄中找到任何**對象,你可以使用'如果文件中的名字或目錄中的名字' – 2014-10-17 23:29:51

+3

小心區分大小寫。 '用於文件中的名稱:''在文件系統中的'super-photo.JPG'時將無法查找'super-photo.jpg'。 (我一生中的一個小時我想回來;-)有點亂的解決方法是'如果str.lower(名稱)在[x。lower()for x in files]' – 2014-12-16 22:53:25

+0

如何使用** yield **而不是準備結果列表? ..... if fnmatch.fnmatch(name,pattern): yield os.path.join(root,name) – Berci 2015-05-03 21:26:13

2

對於快速,獨立於操作系統的搜索,使用scandir

https://github.com/benhoyt/scandir/#readme

http://bugs.python.org/issue11406的細節原因。

+2

具體來說,使用'scandir.walk() per @ Nadia的回答。請注意,如果你使用Python 3.5+,'os.walk()'已經有'scandir.walk()'加速。另外,[PEP 471](https://www.python.org/dev/peps/pep-0471/)可能是一個比這個問題更好的閱讀信息的文檔。 – 2016-12-15 15:29:49

13

我用的os.walk一個版本,並在更大的目錄傳開時間3.5秒。我嘗試了兩種隨機解,沒有很大的改善,那麼就做:

paths = [line[2:] for line in subprocess.check_output("find . -iname '*.txt'", shell=True).splitlines()] 

雖然這是POSIX的唯一,我得到了0.25秒。

由此,我認爲這是完全有可能的,以優化整個搜索了很多獨立於平臺的方式,但是這是我停止了研究。

0

如果您正在使用Python在Ubuntu,你只希望它在Ubuntu的工作基本上更快的方法是利用終端的locate這樣的程序。

import subprocess 

def find_files(file_name): 
    command = ['locate', file_name] 

    output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0] 
    output = output.decode() 

    search_results = output.split('\n') 

    return search_results 

search_results是絕對文件路徑的list。這比上面的方法快10,000倍,而且我做了一次搜索,速度快了72,000倍。

1

如果您正在使用Python 2個工作必須與所造成的自參照符號鏈接窗口無限遞歸的問題。

此腳本將避免以下這些。請注意,這是特定窗口

import os 
from scandir import scandir 
import ctypes 

def is_sym_link(path): 
    # http://stackoverflow.com/a/35915819 
    FILE_ATTRIBUTE_REPARSE_POINT = 0x0400 
    return os.path.isdir(path) and (ctypes.windll.kernel32.GetFileAttributesW(unicode(path)) & FILE_ATTRIBUTE_REPARSE_POINT) 

def find(base, filenames): 
    hits = [] 

    def find_in_dir_subdir(direc): 
     content = scandir(direc) 
     for entry in content: 
      if entry.name in filenames: 
       hits.append(os.path.join(direc, entry.name)) 

      elif entry.is_dir() and not is_sym_link(os.path.join(direc, entry.name)): 
       try: 
        find_in_dir_subdir(os.path.join(direc, entry.name)) 
       except UnicodeDecodeError: 
        print "Could not resolve " + os.path.join(direc, entry.name) 
        continue 

    if not os.path.exists(base): 
     return 
    else: 
     find_in_dir_subdir(base) 

    return hits 

它返回一個列表,其中包含所有指向文件名列表中文件的路徑。 用法:

find("C:\\", ["file1.abc", "file2.abc", "file3.abc", "file4.abc", "file5.abc"]) 
+0

請注意,這是特定窗口 – Leliel 2017-11-29 20:37:12

+0

@Leliel已將其添加到答案。感謝您的反饋意見。 – 2017-11-30 21:08:47