91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

通編碼讀取文件內容

發布時間:2020-07-16 03:37:16 來源:網絡 閱讀:219 作者:知止內明 欄目:編程語言

通編碼讀取文件內容


# 通編碼讀取文件內容
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"))
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

芦山县| 西吉县| 桑日县| 宝应县| 保靖县| 青田县| 明星| 牟定县| 策勒县| 贺兰县| 张家界市| 德令哈市| 肇源县| 澄城县| 苏尼特左旗| 健康| 南溪县| 寿阳县| 林周县| 锡林郭勒盟| 福泉市| 台北县| 炎陵县| 靖边县| 大英县| 玉林市| 务川| 行唐县| 股票| 察哈| 兴海县| 乐东| 甘谷县| 福泉市| 临海市| 和林格尔县| 忻城县| 武陟县| 依安县| 波密县| 定陶县|