python实现大文本文件分割


Posted in Python onJuly 22, 2019

本文实例为大家分享了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<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')

源码地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python通过解析网页实现看报程序的方法
Aug 04 Python
python实现指定字符串补全空格的方法
Apr 30 Python
Python3 Random模块代码详解
Dec 04 Python
python redis 删除key脚本的实例
Feb 19 Python
python tkinter实现界面切换的示例代码
Jun 14 Python
Python格式化字符串f-string概览(小结)
Jun 18 Python
浅谈pycharm使用及设置方法
Sep 09 Python
python连接PostgreSQL数据库的过程详解
Sep 18 Python
python基于FTP实现文件传输相关功能代码实例
Sep 28 Python
Python如何使用argparse模块处理命令行参数
Dec 11 Python
详解python 降级到3.6终极解决方案
Feb 06 Python
Python使用mitmproxy工具监控手机 下载手机小视频
Apr 18 Python
python plotly绘制直方图实例详解
Jul 22 #Python
python分割一个文本为多个文本的方法
Jul 22 #Python
在linux系统下安装python librtmp包的实现方法
Jul 22 #Python
django搭建项目配置环境和创建表过程详解
Jul 22 #Python
对python中基于tcp协议的通信(数据传输)实例讲解
Jul 22 #Python
Django使用中间键实现csrf认证详解
Jul 22 #Python
python Tcp协议发送和接收信息的例子
Jul 22 #Python
You might like
php imagecreatetruecolor 创建高清和透明图片代码小结
2010/05/15 PHP
php学习之数据类型之间的转换介绍
2011/06/09 PHP
PHP人民币金额数字转中文大写的函数代码
2013/02/27 PHP
PHP正则提取不包含指定网址的图片地址的例子
2014/04/21 PHP
php中替换字符串中的空格为逗号','的方法
2014/06/09 PHP
验证坐标在某坐标区域内php代码
2016/10/08 PHP
基于PHP实现短信验证码发送次数限制
2020/07/11 PHP
JQuery 写的个性导航菜单
2009/12/24 Javascript
获取offsetTop和offsetLeft值的js代码(兼容)
2013/04/16 Javascript
js获取事件源及触发该事件的对象
2013/10/24 Javascript
JS实现拖动示例代码
2013/11/01 Javascript
JS获取屏幕,浏览器窗口大小,网页高度宽度(实现代码)
2013/12/17 Javascript
JavaScript取得WEB安全颜色列表的方法
2015/07/14 Javascript
JS区分浏览器页面是刷新还是关闭
2016/04/17 Javascript
微信小程序 解决请求服务器手机预览请求不到数据的方法
2017/01/04 Javascript
Angular directive递归实现目录树结构代码实例
2017/05/05 Javascript
JS将unicode码转中文方法
2017/05/08 Javascript
利用canvas中toDataURL()将图片转为dataURL(base64)的方法详解
2017/11/20 Javascript
Vue 报错TypeError: this.$set is not a function 的解决方法
2018/12/17 Javascript
详解在网页上通过JS实现文本的语音朗读
2019/03/28 Javascript
浅谈Vue3 Composition API如何替换Vue Mixins
2020/04/29 Javascript
python网络编程示例(客户端与服务端)
2014/04/24 Python
python中list循环语句用法实例
2014/11/10 Python
详解Python字符串对象的实现
2015/12/24 Python
分享Python开发中要注意的十个小贴士
2016/08/30 Python
python实现学生信息管理系统
2020/04/05 Python
Python对HTML转义字符进行反转义的实现方法
2019/04/28 Python
python matplotlib库绘制条形图练习题
2019/08/10 Python
基于Python实现船舶的MMSI的获取(推荐)
2019/10/21 Python
解决keras加入lambda层时shape的问题
2020/06/11 Python
Notino希腊:购买香水和美容产品
2019/07/25 全球购物
大学生自助营养快餐店创业计划书
2014/01/13 职场文书
医务工作者先进事迹材料
2014/01/26 职场文书
乡镇组织委员个人整改措施
2014/09/16 职场文书
学雷锋感言
2015/08/03 职场文书
MySQL一劳永逸永久支持输入中文的方法实例
2022/08/05 MySQL