2017-01-13 55 views
0

當我使用sc.textFile('*.txt')時,我會採取一切。PySpark從列表中排除文件

我希望能夠過濾出多個文件。

例如我怎樣才能取得除['bar.txt','foo.txt']之外的所有文件?

+3

的可能的複製[如何使用正則表達式在sc.t中包含/排除某些輸入文件extFile?](http://stackoverflow.com/questions/31782763/how-to-use-regex-to-include-exclude-some-input-files-in-sc-textfile) – Yaron

回答

1

這更多的是一種變通方法:

獲取文件列表:

import os 
file_list = os.popen('hadoop fs -ls <your dir>').readlines() 

過濾它:

file_list = [x for x in file_list if (x not in ['bar.txt','foo.txt') 
      and x[-3:]=='txt'] 

讀吧:

rdd = sc.textFile(['<your dir>/'+x for x in file list])