2017-01-13 50 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])