最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)大文本文件分割

 更新時(shí)間:2019年07月22日 10:15:51   作者:Angryshark_128  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)大文本文件分割,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python實(shí)現(xiàn)大文本文件分割的具體代碼,供大家參考,具體內(nèi)容如下

開(kāi)發(fā)環(huán)境

Python 2

實(shí)現(xiàn)效果

通過(guò)文件拖拽或文件路徑輸入,實(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)文章

最新評(píng)論

宁陕县| 台前县| 长寿区| 射阳县| 若羌县| 马鞍山市| 砀山县| 内乡县| 阿拉善盟| 九江市| 中牟县| 商南县| 临泉县| 阿勒泰市| 沐川县| 诸城市| 安多县| 商城县| 巴彦淖尔市| 石台县| 文昌市| 阿城市| 灵宝市| 高陵县| 凤城市| 灌南县| 资溪县| 宜良县| 绥阳县| 剑河县| 双鸭山市| 洮南市| 万全县| 沧源| 阜康市| 彭山县| 青神县| 芜湖县| 昌黎县| 瑞丽市| 辉县市|