通編碼讀取文件內(nèi)容

通編碼讀取文件內(nèi)容


# 通編碼讀取文件內(nèi)容
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"):
    '''此函數(shù)用于讀取某個(gè)文件的所有行'''
    if os.path.isfile(file_path):
        #判斷file_path參數(shù)是文件的情況
        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參數(shù)是目錄的情況
        print("%s is a dir! can not read content directly!" %file_path)
        return []
    else:
        #判斷file_path參數(shù)即不是目錄,也不是文件的情況
        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):
    """統(tǒng)計(jì)一個(gè)目錄的包含某字符串的行數(shù)
       path參數(shù)可以是目錄路徑也可以是文件路徑"""    
    line_nums = 0

    if not os.path.exists(path):
        #判斷路徑在不在,不在的話返回0
        print("%s does not exists!" %path)
        return line_nums
    elif os.path.isfile(path):
        #當(dāng)路徑是文件的時(shí)候,用封裝的read_lines_from_file
        #讀取所有行,然后在做計(jì)數(shù)
        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):
        #當(dāng)路徑是目錄的時(shí)候,用封裝的read_lines_from_file
        #讀取所有行,然后在做計(jì)數(shù)
        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):
    """統(tǒng)計(jì)一個(gè)目錄的包含某字符串的所有行
       path參數(shù)可以是目錄路徑也可以是文件路徑"""    

    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"):
        '''此函數(shù)用于讀取某個(gè)文件的所有行'''
        if os.path.isfile(file_path):
            # 判斷file_path參數(shù)是文件的情況
            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參數(shù)是目錄的情況
            print("%s is a dir! can not read content directly!" % file_path)
            return []
        else:
            # 判斷file_path參數(shù)即不是目錄,也不是文件的情況
            print("%s file path does not exist!" % file_path)
            return []

    @staticmethod
    def read_lines_from_file(file_path, coding="utf-8"):
        '''此函數(shù)用于讀取某個(gè)文件的所有行'''
        if os.path.isfile(file_path):
            # 判斷file_path參數(shù)是文件的情況
            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參數(shù)是目錄的情況
            print("%s is a dir! can not read content directly!" % file_path)
            return []
        else:
            # 判斷file_path參數(shù)即不是目錄,也不是文件的情況
            print("%s file path does not exist!" % file_path)
            return []

    @staticmethod
    def count_line_num(path, match_letters):
        """統(tǒng)計(jì)一個(gè)目錄的包含某字符串的行數(shù)
           path參數(shù)可以是目錄路徑也可以是文件路徑"""
        line_nums = 0

        if not os.path.exists(path):
            # 判斷路徑在不在,不在的話返回0
            print("%s does not exists!" % path)
            return line_nums
        elif os.path.isfile(path):
            # 當(dāng)路徑是文件的時(shí)候,用封裝的read_lines_from_file
            # 讀取所有行,然后在做計(jì)數(shù)
            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):
            # 當(dāng)路徑是目錄的時(shí)候,用封裝的read_lines_from_file
            # 讀取所有行,然后在做計(jì)數(shù)
            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):
        """統(tǒng)計(jì)一個(gè)目錄的包含某字符串的所有行
           path參數(shù)可以是目錄路徑也可以是文件路徑"""

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

文章標(biāo)題:通編碼讀取文件內(nèi)容
瀏覽路徑:http://m.kartarina.com/article2/geceic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司電子商務(wù)Google商城網(wǎng)站網(wǎng)站排名企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站
主站蜘蛛池模板: 成人无码A区在线观看视频| 亚洲中文字幕无码爆乳app| 人妻无码中文字幕| av无码人妻一区二区三区牛牛| 国产网红无码精品视频| 久久无码人妻一区二区三区| 国产精品国产免费无码专区不卡 | 岛国无码av不卡一区二区| 亚洲av日韩av高潮潮喷无码| 无码人妻啪啪一区二区| 亚洲V无码一区二区三区四区观看 亚洲爆乳精品无码一区二区三区 亚洲爆乳无码一区二区三区 | 在线无码视频观看草草视频| 日韩综合无码一区二区| 无码国产精品一区二区免费式影视| 亚洲AV永久无码精品一区二区国产 | 久久久久亚洲AV无码专区首JN | 中文字幕人妻三级中文无码视频| 无码日韩人妻精品久久蜜桃| 国产高清无码二区| 无码高潮爽到爆的喷水视频app| 久久无码专区国产精品| 成人av片无码免费天天看| 东京热av人妻无码| 亚洲成a∨人片在无码2023| 无码人妻丰满熟妇精品区| 日本爆乳j罩杯无码视频| 国产精品va无码二区| 97无码免费人妻超级碰碰碰碰| 亚洲欧洲av综合色无码| 亚洲AV无码国产精品色| 乱色精品无码一区二区国产盗 | 91久久精品无码一区二区毛片| 亚洲AV无码国产丝袜在线观看 | 精品久久久久久无码国产| 亚洲AV无码国产精品麻豆天美 | 久久午夜夜伦鲁鲁片无码免费| 无码人妻精品内射一二三AV| JAVA性无码HD中文| 日韩av无码国产精品| 国产莉萝无码AV在线播放| 亚洲欧洲av综合色无码|