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 相关文章推荐
在Gnumeric下使用Python脚本操作表格的教程
Apr 14 Python
浅析Python中else语句块的使用技巧
Jun 16 Python
Python自动化测试ConfigParser模块读写配置文件
Aug 15 Python
Python 中pandas.read_excel详细介绍
Jun 23 Python
Python模拟三级菜单效果
Sep 11 Python
python pexpect ssh 远程登录服务器的方法
Feb 14 Python
Python定义函数功能与用法实例详解
Apr 08 Python
Python对HTML转义字符进行反转义的实现方法
Apr 28 Python
用pytorch的nn.Module构造简单全链接层实例
Jan 14 Python
python实现信号时域统计特征提取代码
Feb 26 Python
利用keras使用神经网络预测销量操作
Jul 07 Python
浅谈tf.train.Saver()与tf.train.import_meta_graph的要点
May 26 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
数据库中排序的对比及使用条件详解
2012/02/23 PHP
php实现ip白名单黑名单功能
2015/03/12 PHP
php实现兼容2038年后Unix时间戳转换函数
2015/03/18 PHP
php版微信公众平台回复中文出现乱码问题的解决方法
2016/09/22 PHP
php对xml文件的增删改查操作实现方法分析
2017/05/19 PHP
JS分页效果示例
2013/10/11 Javascript
jQuery实现垂直半透明手风琴特效代码分享
2015/08/21 Javascript
基于Jquery实现万圣节快乐特效
2015/11/01 Javascript
javascript省市区三级联动下拉框菜单实例演示
2015/11/29 Javascript
通过node-mysql搭建Windows+Node.js+MySQL环境的教程
2016/03/01 Javascript
C#微信小程序服务端获取用户解密信息实例代码
2017/03/10 Javascript
vue 实现边输入边搜索功能的实例讲解
2018/09/16 Javascript
angular 实现下拉列表组件的示例代码
2019/03/09 Javascript
Postman内建变量常用方法实例解析
2020/07/28 Javascript
angular8.5集成TinyMce5的使用和详细配置(推荐)
2020/11/16 Javascript
python中使用pyhook实现键盘监控的例子
2014/07/18 Python
使用相同的Apache实例来运行Django和Media文件
2015/07/22 Python
Python数据结构之单链表详解
2017/09/12 Python
Django 实现购物车功能的示例代码
2018/10/08 Python
使用pycharm设置控制台不换行的操作方法
2019/01/19 Python
使用python获取(宜宾市地震信息)地震信息
2019/06/20 Python
python sorted方法和列表使用解析
2019/11/18 Python
Python datetime模块使用方法小结
2020/06/18 Python
HTML5 预加载让页面得以快速呈现
2013/08/13 HTML / CSS
Smallable意大利家庭概念店:设计师童装及家居装饰
2018/01/08 全球购物
Peter Millar官网:美国高档生活服饰品牌
2018/07/02 全球购物
美国Jeep配件购物网站:Morris 4×4 Center
2019/05/01 全球购物
个人实用简单的自我评价
2013/10/19 职场文书
《山谷中的谜底》教学反思
2014/04/26 职场文书
工作保证书范文
2014/04/29 职场文书
优秀管理者事迹材料
2014/05/22 职场文书
办公室务虚会发言材料
2014/10/20 职场文书
2015年小学数学教师工作总结
2015/05/20 职场文书
《平移和旋转》教学反思
2016/02/19 职场文书
长辈生日祝福语大全(72句)
2019/08/09 职场文书
goland 清除所有的默认设置操作
2021/04/28 Golang