Python读取一目录下的所有文件

原文地址: https://blog.csdn.net/Scythe666/article/details/90262029

import os
path = "D:/DATA" #文件夹目录
files= os.listdir(path) #得到文件夹下的所有文件名称
s = []
for file in files: #遍历文件夹
    if not os.path.isdir(file): #判断是否是文件夹,不是文件夹才打开
        f = open(path+"/"+file); #打开文件
        iter_f = iter(f); #创建迭代器
        str = ""
        for line in iter_f: #遍历文件,一行行遍历,读取文本
            str = str + line
        s.append(str) #每个文件的文本存到list中
print(s) #打印结果
posted @ 2020-04-14 16:29  云远·笨小孩  阅读(10633)  评论(0编辑  收藏  举报