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



我想打开文件夹中的所有文档,并将每个文档放入列表的元素中,如:["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

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

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

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

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

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')

相关内容

  • 没有找到相关文章

最新更新