日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python實現大文本文件分割

系統 1773 0

本文實例為大家分享了python實現大文本文件分割的具體代碼,供大家參考,具體內容如下

開發環境

Python 2

實現效果

通過文件拖拽或文件路徑輸入,實現自定義大文本文件分割。

代碼實現

            
 #coding:gbk
 import os,sys,shutil
 
 is_file_exits=False
 while not is_file_exits:
  files_list=[]
  if(len(sys.argv)==1):
   print('請輸入要切割的文件完整路徑:')
   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()+'文件路徑不存在,請重新輸入!')
     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()+'文件路徑不存在,請重新輸入!')
     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('請輸入要切割的文件個數:')
  str_files_count=raw_input()
  if str_files_count.isdigit():
   is_continue=True
  else:
   print('請輸入正確的數字!')
 
 for file_path in files_list:
 
  split_file_path=''
  total_lines_count=0
  lines_count=0
  files_count=int(str_files_count)
 
  print('正在統計文本行數.....')
 
  total_lines_count = len(open(file_path,'rU').readlines())
  print('文本總行數:'+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('正在進行文件分割.....')
 
  line_num=0
  file_num=0
  temp=-1
 
  for line in open(file_path,'rU').readlines():
   if file_num
            
            
          

源碼地址

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 南丹县| 乌什县| 武城县| 通州市| 惠水县| 全南县| 疏勒县| 乐亭县| 东城区| 静乐县| 苗栗市| 定州市| 千阳县| 邛崃市| 海盐县| 清镇市| 玉龙| 固原市| 连平县| 灵台县| 大关县| 安顺市| 镇宁| 淮阳县| 永州市| 临漳县| 昌乐县| 台北市| 庆云县| 黄龙县| 图木舒克市| 庆阳市| 大丰市| 沂南县| 淮滨县| 肇庆市| 宝兴县| 岑巩县| 屏山县| 长阳| 蓬安县|