python實現(xiàn)按行分割文件
本文實例為大家分享了python實現(xiàn)按行分割文件的具體代碼,供大家參考,具體內(nèi)容如下
#!/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 : # 正常結(jié)束循環(huán)后將剩余的內(nèi)容寫入新文件中
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):
"""將按行分割后的內(nèi)容寫入相應的分割文件中"""
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')
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Python3.74+PyCharm2020.1 x64中安裝使用Kivy的詳細教程
這篇文章主要介紹了在Python3.74+PyCharm2020.1 x64中安裝使用Kivy的詳細教程,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
python linecache 處理固定格式文本數(shù)據(jù)的方法
今天小編就為大家分享一篇python linecache 處理固定格式文本數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
python數(shù)字圖像處理skimage讀取顯示與保存圖片
這篇文章主要為大家介紹了python數(shù)字圖像處理使用skimage讀取顯示與保存圖片示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
python實現(xiàn)批量轉(zhuǎn)換文件編碼(批轉(zhuǎn)換編碼示例)
這篇文章主要介紹了python實現(xiàn)批量轉(zhuǎn)換文件編碼示例,指定文件編碼、目錄或擴展名即可進行轉(zhuǎn)換,大家參考使用吧2014-01-01
python使用matplotlib畫出的圖怎樣放到word中
這篇文章主要介紹了python使用matplotlib畫出的圖怎樣放到word中問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
django+celery+RabbitMQ自定義多個消息隊列的實現(xiàn)
本文主要介紹了django+celery+RabbitMQ自定義多個消息隊列的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02
keras中模型訓練class_weight,sample_weight區(qū)別說明
這篇文章主要介紹了keras中模型訓練class_weight,sample_weight區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05

