python如何从一个文件夹中读取多个.dat文件

python如何从一个文件夹中读取多个.dat文件,第1张

用glob模块,指定后缀.dat,即可。

import glob

dir = '\home\your_data_file\'

for f in glob.glob(dir + '*.dat'):

    contents = open(f,'r').read()

import os

def search(s, path=os.path.abspath('.')):

    for z in os.listdir(path):

        if os.path.isdir(path + os.path.sep + z):

            print('Currnet:', path)

            path2 = os.path.join(path, z)

            print('future:', path2)

            search(s, path2)

        elif os.path.isfile(path + os.path.sep + z):

            if s in z:

                print(os.path.join(path, z))

                with open(path + os.path.sep + z, 'r') as fr:

                    with open('save.txt', 'a') as fw:

                        fw.write(path + '\t' + fr.read())

search('csv', '.')

#!/usr/bin/env python3.6

from pathlib import Path

def read_all_txt(dirname):

    ss = []

    for p in Path(dirname).rglob('*.txt'):

        ss.append(p.read_text())

    return ss


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12098356.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存