通编码读取文件内容
admin
2023-01-29 00:20:04
0

通编码读取文件内容


# 通编码读取文件内容
def read_lines_from_file(file_path, coding="utf-8"):
    line_content = []
    if os.path.isfile(file_path):
        try:
            with open(file_path, encoding=coding) as fp:
                line_content = fp.readlines()
            return line_content
        except Exception as e:
            # print(e)
            try:
                with open(file_path, encoding="gbk") as fp:
                    line_content = fp.readlines()
                return line_content
            except Exception as e:
                print(e)
                return []
    elif os.path.isdir(file_path):
        print("%s is a dir! can not read content directly!" % file_path)
        return []
    else:
        print("%s file path does not exist!" % file_path)
        return []

升级

import os.path

def read_lines_from_file(file_path,coding="utf-8"):
    '''此函数用于读取某个文件的所有行'''
    if os.path.isfile(file_path):
        #判断file_path参数是文件的情况
        try:
            #用utf-8编码去读取文件的所有行
            with open(file_path,encoding=coding) as fp:
                line_content = fp.readlines()               
            return line_content
        except Exception as e:
            #print(e)
            #用utf-8编码读取出异常后,用gbk去读取文件的所有行
            try:
                with open(file_path,encoding="gbk") as fp:
                    line_content = fp.readlines()  
                return line_content
            except Exception as e:
                print(e)
                return []
    elif os.path.isdir(file_path):
        #判断file_path参数是目录的情况
        print("%s is a dir! can not read content directly!" %file_path)
        return []
    else:
        #判断file_path参数即不是目录,也不是文件的情况
        print("%s file path does not exist!" %file_path)
        return []

#print(read_lines_from_file("e:\\笔记1.txt"))
#print(read_lines_from_file("e:\\test1111"))

def count_line_num(path,match_letters):
    """统计一个目录的包含某字符串的行数
       path参数可以是目录路径也可以是文件路径"""    
    line_nums = 0

    if not os.path.exists(path):
        #判断路径在不在,不在的话返回0
        print("%s does not exists!" %path)
        return line_nums
    elif os.path.isfile(path):
        #当路径是文件的时候,用封装的read_lines_from_file
        #读取所有行,然后在做计数
        if ".txt" not in path:
            return line_nums
        for line in read_lines_from_file(path):
            if match_letters in line:
                line_nums+=1
        return line_nums
    elif os.path.isdir(path):
        #当路径是目录的时候,用封装的read_lines_from_file
        #读取所有行,然后在做计数
        for root,dirs,files in os.walk(path):
            for file in files:
                if ".txt" not in file:
                    continue
                file_path = os.path.join(root,file)
                for line in read_lines_from_file(file_path):
                    if match_letters in line:
                        line_nums+=1
        return line_nums

def get_specific_lines(path,match_letters):
    """统计一个目录的包含某字符串的所有行
       path参数可以是目录路径也可以是文件路径"""    

    specific_lines =[]
    if not os.path.exists(path):
        print("%s does not exists!" %path)
        return line_nums
    elif os.path.isfile(path):
        if ".txt" not in path:
            return line_nums
        for line in read_lines_from_file(path):
            if match_letters in line:
                specific_lines.append(line)
        return line_nums
    elif os.path.isdir(path):
        for root,dirs,files in os.walk(path):
            for file in files:
                if ".txt" not in file:
                    continue
                file_path = os.path.join(root,file)
                for line in read_lines_from_file(file_path):
                    if match_letters in line:
                        specific_lines.append(line)
        return specific_lines
#print(count_line_num("e:\\a.txt","ab"))
print(get_specific_lines("e:\\pic","ab"))

with  open(r"e:\result.txt",'w') as fp:
    fp.writelines(get_specific_lines("e:\\pic","ab"))

再次修改:

import os.path

class Data:

    def __init__(self,path):
        self.path =path

    @staticmethod
    def  read_lines_from_file(file_path, coding="utf-8"):
        '''此函数用于读取某个文件的所有行'''
        if os.path.isfile(file_path):
            # 判断file_path参数是文件的情况
            try:
                # 用utf-8编码去读取文件的所有行
                with open(file_path, encoding=coding) as fp:
                    line_content = fp.readlines()
                return line_content
            except Exception as e:
                # print(e)
                # 用utf-8编码读取出异常后,用gbk去读取文件的所有行
                try:
                    with open(file_path, encoding="gbk") as fp:
                        line_content = fp.readlines()
                    return line_content
                except Exception as e:
                    print(e)
                    return []
        elif os.path.isdir(file_path):
            # 判断file_path参数是目录的情况
            print("%s is a dir! can not read content directly!" % file_path)
            return []
        else:
            # 判断file_path参数即不是目录,也不是文件的情况
            print("%s file path does not exist!" % file_path)
            return []

    @staticmethod
    def read_lines_from_file(file_path, coding="utf-8"):
        '''此函数用于读取某个文件的所有行'''
        if os.path.isfile(file_path):
            # 判断file_path参数是文件的情况
            try:
                # 用utf-8编码去读取文件的所有行
                with open(file_path, encoding=coding) as fp:
                    line_content = fp.readlines()
                return line_content
            except Exception as e:
                # print(e)
                # 用utf-8编码读取出异常后,用gbk去读取文件的所有行
                try:
                    with open(file_path, encoding="gbk") as fp:
                        line_content = fp.readlines()
                    return line_content
                except Exception as e:
                    print(e)
                    return []
        elif os.path.isdir(file_path):
            # 判断file_path参数是目录的情况
            print("%s is a dir! can not read content directly!" % file_path)
            return []
        else:
            # 判断file_path参数即不是目录,也不是文件的情况
            print("%s file path does not exist!" % file_path)
            return []

    @staticmethod
    def count_line_num(path, match_letters):
        """统计一个目录的包含某字符串的行数
           path参数可以是目录路径也可以是文件路径"""
        line_nums = 0

        if not os.path.exists(path):
            # 判断路径在不在,不在的话返回0
            print("%s does not exists!" % path)
            return line_nums
        elif os.path.isfile(path):
            # 当路径是文件的时候,用封装的read_lines_from_file
            # 读取所有行,然后在做计数
            if ".txt" not in path:
                return line_nums
            for line in Data.read_lines_from_file(path):
                if match_letters in line:
                    line_nums += 1
            return line_nums
        elif os.path.isdir(path):
            # 当路径是目录的时候,用封装的read_lines_from_file
            # 读取所有行,然后在做计数
            for root, dirs, files in os.walk(path):
                for file in files:
                    if ".txt" not in file:
                        continue
                    file_path = os.path.join(root, file)
                    for line in read_lines_from_file(file_path):
                        if match_letters in line:
                            line_nums += 1
            return line_nums

    @staticmethod
    def get_specific_lines(path, match_letters):
        """统计一个目录的包含某字符串的所有行
           path参数可以是目录路径也可以是文件路径"""

        specific_lines = []
        if not os.path.exists(path):
            print("%s does not exists!" % path)
            return specific_lines
        elif os.path.isfile(path):
            if ".txt" not in path:
                return []
            for line in Data.read_lines_from_file(path):
                if match_letters in line:
                    specific_lines.append(line)
            return specific_lines
        elif os.path.isdir(path):
            for root, dirs, files in os.walk(path):
                for file in files:
                    if ".txt" not in file:
                        continue
                    file_path = os.path.join(root, file)
                    for line in read_lines_from_file(file_path):
                        if match_letters in line:
                            specific_lines.append(line)
            return specific_lines

print(Data.read_lines_from_file("e:\\a.txt"))
print(Data.count_line_num("e:\\a.txt","ab"))
print(Data.get_specific_lines("e:\\a.txt","ab"))

相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...