2017-08-10 25 views
3

何百万ものファイルパスの存在をチェックし、存在しないパスを特定するための最良の方法は何ですか?私は現在、次のような単一のスレッドを使用しています。複数のファイルの存在を確認する最も簡単な方法

paths_not_existing = set() 
all_file_paths = [long_list] 
for path_name in all_file_paths: 
    if os.path.isfile(path_name) == False: 
     paths_not_existing.add(path_name) 

マルチスレッド化がスピードアップする可能性はありますか?具体的には、これはI/Oバウンドだと仮定しているので、複数のパスを同時に検索する方法があるのだろうか?

(参考のため、私が使用しているハードドライブはソリッドステートではありません)。

+0

... ' nonexistent_pathsは= {(PATH_NAME)をos.path.isfileない場合all_file_pathsでPATH_NAMEためPATH_NAME}。または、フィルタリングする。 'nonexistent_paths = set(フィルタ(lambda f:os.path.isfile(f)、all_file_paths))' – erip

+0

これらのパスがツリー内にある場合は、すでにパスが見つからない方が効率的です親が存在しないことを立証した。 –

答えて

0

あなたは確かにマルチスレッド/処理を使用することができ、それはあなたにスピードアップを与えるはずです。さまざまな方法がありますが、もっとも簡単なのはおそらくmultiprocessing.Pool.mapで、これはPythonのmap関数でビルドされたものと同じように動作しますが、コアを介して配布されています。 `物事をスピードアップ理解を使用

from multiprocessing import Pool 
import numpy as np 
ncores = #number of cores, e.g. 8 
pool = Pool(ncores) 

all_file_paths = np.array(long_array) 

# create a list of booleans corresponding to whether 
# each file is in your path or not. 
selector = np.array(pool.map(os.path.isfile,all_file_paths)) 

paths_not_existing = all_file_paths[selector] 
関連する問題