打开文件夹中的所有文档并将其放入列表中

时间:2014-04-22 08:56:47

标签: python file document

我想打开文件夹中的所有文档,并将每个文档都放入列表中的元素,如:["doc1", "doc2", "doc3", ... ]

for dirpath, dirs, files in os.walk('filepath'):
    for filename in fnmatch.filter(files, '*-*'):
        with open(os.path.join(dirpath, filename)) as text:
            lines=text.readlines()
print lines

但是,它只是将文档的所有内容都变成一个字符串。

请告诉我如何达到预期的结果。

2 个答案:

答案 0 :(得分:2)

docs = []
for dirpath, dirs, files in os.walk('filepath'):
    for filename in fnmatch.filter(files, '*-*'):
        with open(os.path.join(dirpath, filename)) as text:
            docs.append(text.read())
print docs

答案 1 :(得分:0)

从文件夹中获取不同文件类型路径的通用玻璃

idir = "c:\\data\\"  #In directory

class Paths_to_files:
    def __init__(self, idir):
        self.idir = idir
    def get_paths(self, rf):
        rf_list = []
        listing = os.listdir(self.idir) 
        for files in listing:
            if rf in files:
                rf_list.append(files)
        return sorted(rf_list)

class Wrapper(Paths_to_files):      
    def __init__(self, idir):
        self.idir = idir

#Get all files:
files = Wrapper(idir)

#Get all tiff files:
tifpath = files.get_paths('tif')

#Get all csv files:
csvpath = files.get_paths('csv')