python實現(xiàn)按行分割文件
更新時間:2019年07月22日 10:42:33 作者:Jredreamer
這篇文章主要為大家詳細介紹了python如何實現(xiàn)按行分割文件,python按指定行數(shù)分割文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python實現(xiàn)按行分割文件的具體代碼,供大家參考,具體內容如下
#!/usr/bin/env python #--*-- coding:utf-8 --*-- import os class SplitFiles(): """按行分割文件""" def __init__(self, file_name, line_count=200): """初始化要分割的源文件名和分割后的文件行數(shù)""" self.file_name = file_name self.line_count = line_count def split_file(self): if self.file_name and os.path.exists(self.file_name): try: with open(self.file_name) as f : # 使用with讀文件 temp_count = 0 temp_content = [] part_num = 1 for line in f: if temp_count < self.line_count: temp_count += 1 else : self.write_file(part_num, temp_content) part_num += 1 temp_count = 1 temp_content = [] temp_content.append(line) else : # 正常結束循環(huán)后將剩余的內容寫入新文件中 self.write_file(part_num, temp_content) except IOError as err: print(err) else: print("%s is not a validate file" % self.file_name) def get_part_file_name(self, part_num): """"獲取分割后的文件名稱:在源文件相同目錄下建立臨時文件夾temp_part_file,然后將分割后的文件放到該路徑下""" temp_path = os.path.dirname(self.file_name) # 獲取文件的路徑(不含文件名) part_file_name = temp_path + "temp_part_file" if not os.path.exists(temp_path) : # 如果臨時目錄不存在則創(chuàng)建 os.makedirs(temp_path) part_file_name += os.sep + "temp_file_" + str(part_num) + ".part" return part_file_name def write_file(self, part_num, *line_content): """將按行分割后的內容寫入相應的分割文件中""" part_file_name = self.get_part_file_name(part_num) print(line_content) try : with open(part_file_name, "w") as part_file: part_file.writelines(line_content[0]) except IOError as err: print(err) if __name__ == "__main__": sf = SplitFiles(r"F:\multiple_thread_read_file.txt") sf.split_file()
小編再為大家分享一段代碼:
將文本文件按照指定的行數(shù)分割成數(shù)個小的文本文件
#! /usr/bin/env python # -*- coding: utf-8 -*- LIMIT=1000 file_count=0 url_list=[] with open("123.txt") as f: for line in f: url_list.append(line) if len(url_list)<LIMIT: continue #數(shù)據(jù)達到LIMIT file_name=str(file_count)+".txt" with open(file_name,'w') as file: for url in url_list[:-1]: #print(url) file.write(url) file.write(url_list[-1].strip()) url_list=[] file_count+=1 if url_list: file_name=str(file_count)+".txt" with open(file_name,'w') as file: for url in url_list: file.write(url) print('done')
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python實現(xiàn)的對本地host127.0.0.1主機進行掃描端口功能示例
這篇文章主要介紹了Python實現(xiàn)的對本地host127.0.0.1主機進行掃描端口功能,可實現(xiàn)掃描本機開放端口的功能,涉及Python socket模塊與Thread多線程模塊相關使用技巧,需要的朋友可以參考下2019-02-02opencv python統(tǒng)計及繪制直方圖的方法
這篇文章主要介紹了opencv python統(tǒng)計及繪制直方圖的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01Python3 實現(xiàn)減少可調用對象的參數(shù)個數(shù)
今天小編就為大家分享一篇Python3 實現(xiàn)減少可調用對象的參數(shù)個數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python Opencv中用compareHist函數(shù)進行直方圖比較對比圖片
這篇文章主要介紹了Python Opencv中用compareHist函數(shù)進行直方圖比較進行對比圖片,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04python框架flask入門之路由及簡單實現(xiàn)方法
這篇文章主要介紹了python框架flask入門路由及路由簡單實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06