python實(shí)現(xiàn)大文本文件分割
本文實(shí)例為大家分享了python實(shí)現(xiàn)大文本文件分割的具體代碼,供大家參考,具體內(nèi)容如下
開發(fā)環(huán)境
Python 2
實(shí)現(xiàn)效果
通過文件拖拽或文件路徑輸入,實(shí)現(xiàn)自定義大文本文件分割。
代碼實(shí)現(xiàn)
#coding:gbk
import os,sys,shutil
is_file_exits=False
while not is_file_exits:
files_list=[]
if(len(sys.argv)==1):
print('請(qǐng)輸入要切割的文件完整路徑:')
files_path=raw_input().strip()
for str_file_path in files_path.split(' '):
if(str_file_path.strip()==''):
continue
if(not os.path.exists(str_file_path.strip())):
print(str_file_path.strip()+'文件路徑不存在,請(qǐng)重新輸入!')
is_file_exits=False
break
else:
files_list.append(str_file_path.strip());
is_file_exits=True
else:
for str_file_path in sys.argv[1:len(sys.argv)]:
if(str_file_path.strip()==''):
continue
if(not os.path.exists(str_file_path.strip())):
print(str_file_path.strip()+'文件路徑不存在,請(qǐng)重新輸入!')
is_file_exits=False
break
else:
files_list.append(str_file_path.strip());
is_file_exits=True
print('待切割文件:'+str(files_list))
is_continue=False
while not is_continue:
print('請(qǐng)輸入要切割的文件個(gè)數(shù):')
str_files_count=raw_input()
if str_files_count.isdigit():
is_continue=True
else:
print('請(qǐng)輸入正確的數(shù)字!')
for file_path in files_list:
split_file_path=''
total_lines_count=0
lines_count=0
files_count=int(str_files_count)
print('正在統(tǒng)計(jì)文本行數(shù).....')
total_lines_count = len(open(file_path,'rU').readlines())
print('文本總行數(shù):'+str(total_lines_count))
if files_count>total_lines_count:
print('文本太小,不值得分割!')
sys.exit()
(filepath,filename) = os.path.split(file_path);
(filepathname,extension) = os.path.splitext(file_path)
if os.path.exists(filepathname):
shutil.rmtree(filepathname)
os.mkdir(filepathname)
lines_count=int(total_lines_count/files_count)
mod_count=total_lines_count%files_count
print('正在進(jìn)行文件分割.....')
line_num=0
file_num=0
temp=-1
for line in open(file_path,'rU').readlines():
if file_num<mod_count:
file_num=int(line_num/(lines_count+1))
else:
file_num=int((line_num-mod_count*(lines_count+1))/lines_count+mod_count)
split_file_path=filepathname+'/'+str.replace(filename,extension,'_'+str(file_num)+extension)
with open(split_file_path,'a+') as split_file:
split_file.write(line)
if temp!=file_num:
print('正在生成:'+split_file_path)
temp=file_num
line_num+=1
print(file_path+'分割完成!')
split_file.close()
os.system('pause')
源碼地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡(jiǎn)單介紹Python的Django框架加載模版的方式
這篇文章主要介紹了Python的Django框架加載模版的方式,包括一些對(duì)加載順序的介紹,需要的朋友可以參考下2015-07-07
Python matplotlib繪制圖形實(shí)例(包括點(diǎn),曲線,注釋和箭頭)
這篇文章主要介紹了Python matplotlib繪制圖形實(shí)例(包括點(diǎn),曲線,注釋和箭頭),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python爬蟲基礎(chǔ)之selenium庫(kù)的用法總結(jié)
今天帶大家來學(xué)習(xí)selenium庫(kù)的使用方法及相關(guān)知識(shí)總結(jié),文中非常詳細(xì)的介紹了selenium庫(kù),對(duì)正在學(xué)習(xí)python的小伙伴很有幫助,需要的朋友可以參考下2021-05-05
python cookielib 登錄人人網(wǎng)的實(shí)現(xiàn)代碼
今天晚上不是很忙,所以早早的就在電腦的旁邊開始寫東西了。我今天給大家分享一個(gè)我自己用python寫的自動(dòng)登錄 人人網(wǎng)的腳本,沒辦法就是懶!懶的輸入帳號(hào)和密碼,讓python給我們減少工作量2012-12-12
Python 識(shí)別12306圖片驗(yàn)證碼物品的實(shí)現(xiàn)示例
這篇文章主要介紹了Python 識(shí)別12306圖片驗(yàn)證碼物品的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Python爬蟲庫(kù)requests-html進(jìn)行HTTP請(qǐng)求HTML解析等高級(jí)功能應(yīng)用
這篇文章主要為大家介紹了Python爬蟲庫(kù)requests-html進(jìn)行HTTP請(qǐng)求HTML解析JavaScript渲染以及更高級(jí)的功能應(yīng)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12

