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

python實現大文本文件分割

系統 1857 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元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 平江县| 宁晋县| 鹤山市| 旬阳县| 体育| 井研县| 祥云县| 恩平市| 连云港市| 东阳市| 西青区| 尼玛县| 汶上县| 馆陶县| 和田市| 柘荣县| 华蓥市| 宁安市| 兴宁市| 吉隆县| 吐鲁番市| 光山县| 房产| 石嘴山市| 翁牛特旗| 龙口市| 长春市| 阜南县| 临沂市| 黑河市| 囊谦县| 陇南市| 通渭县| 玉山县| 宣城市| 宜川县| 桓台县| 南部县| 开鲁县| 镇远县| 阜平县|