1. 将大文件拆分为小文件

I 通过二进制的方式将大文件读取出来,将其拆分存,以不同的文件方式存放在一个目录下面

II 提供两种操作方式交互式和命令行模式

#! usr/bin/python
# -*- coding:utf-8 -*- import sys, os megebytes = 1024 * 1000
chunksize = int(1.4 * megebytes) def clear_dir(target_dir):
"""
清空目录
:param targetdir:需要清空的目录
:return: None
"""
for fname in os.listdir(target_dir):
path = os.path.join(target_dir, fname)
if os.path.isfile(path):
os.remove(path)
else:
os.rmdir(path) def split(fromfile, todir, chunksize=chunksize):
if not os.path.exists(todir):
os.mkdir(todir)
else:
clear_dir(todir)
partnum = 0 with open(fromfile, "rb") as input:
while True:
tmpdata = input.read(chunksize)
if not tmpdata:break
partnum += 1
filename = os.path.join(todir, ('part{0:04d}'.format(partnum)))
with open(filename, 'wb') as fileobj:
fileobj.write(tmpdata) assert partnum <= 9999
return partnum def main():
global chunksize
if len(sys.argv) == 2 and sys.argv[1] == '-help':
print('Use:split_file.py [file-to-split target-dir [chunksize]]')
else:
if len(sys.argv) < 3:
interactive = True
fromfile = input('enter the file to split:')
todir = input('enter the dir to hold the split info:')
else:
interactive = False
fromfile, todir = sys.argv[1:3]
if len(sys.argv) == 4:chunksize = int(sys.argv[3]) absfrom, absto = map(os.path.abspath, [fromfile, todir])
print('spliting from {0} to {1} by {2}'.format(absfrom, absto, chunksize)) try:
parts = split(absfrom, absto, chunksize)
except:
print('error during split')
else:
print('split finished:{0} parts are in {1}'.format(parts, absto))
if interactive: print('input any key')
if __name__ == '__main__':
#clear_dir("../testdir")
#split("../testdir1/test.pdf", "../testdir")
main()

 2 将拆分之后的文件重新合并

I 将拆分后的文件以二进制的方式读取,再以二进制的方式保存

II 提供两种操作方式交互式和命令行模式

import sys
import os readsize = 1024 def join(fromdir, tofile):
"""
将使用split_file分开的文件重新合并为原文件
:param fromdir: 分开的小文件
:param tofile: 原文件
:return:
""" partfiles = os.listdir(fromdir)
with open(tofile, 'wb') as output:
for eachpart in partfiles:
filepath = os.path.join(fromdir, eachpart)
with open(filepath, 'rb') as fileobj:
while True:
bytes = fileobj.read(readsize)
if not bytes:break
output.write(bytes) if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == '-help':
print('using join [from dir nme] [to file name]')
else:
if len(sys.argv) != 3:
fromdir = input('Enter the from dir')
tofile = input('Enter the to file')
else:
fromdir = sys.argv[1]
tofile = sys.argv[2] fromdir, tofile = map(os.path.abspath, [fromdir, tofile])
print('joining') try:
join(fromdir, tofile)
except:
print("Error during joining file")
else:
print("joining completed")

python 小程序大文件的拆分合并的更多相关文章

  1. Python逐块读取大文件行数的代码 - 为程序员服务

    Python逐块读取大文件行数的代码 - 为程序员服务 python数文件行数最简单的方法是使用enumerate方法,但是如果文件很大的话,这个方法就有点慢了,我们可以逐块的读取文件的内容,然后按块 ...

  2. Python 小程序,对文件操作及其它

    以下是自己写的几个对文件操作的小程序,里面涉及到文件操作,列表(集合,字典)的运用等.比方说,从文件里读取一行数据.分别存放于列表中,再对列表进行操作.如去掉里面的反复项.排序等操作. 常见对文件里行 ...

  3. 怎么样通过编写Python小程序来统计测试脚本的关键字

    怎么样通过编写Python小程序来统计测试脚本的关键字 通常自动化测试项目到了一定的程序,编写的测试代码自然就会很多,如果很早已经编写的测试脚本现在某些基础函数.业务函数需要修改,那么势必要找出那些引 ...

  4. Day1:第一个python小程序

    Day1:第一个python小程序与开发工具Pycharm 一.Hello World C:\Users\wenxh>python Python 3.6.2 (v3.6.2:5fd33b5, J ...

  5. python处理分隔大文件

    4个.sql格式的文件,2G大小,直接插入mysql数据中,文件太大了,导入不进去. 太大的文件用python处理也很麻烦,处理不了,只能先分隔成小文件处理. 文件中数据格式:其中values里面的数 ...

  6. 一个有意思的Python小程序(全国省会名称随机出题)

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 最近比较迷Python,仿照<Python编程快速上手>8.5写了一个随机出卷的小 ...

  7. 小程序-图片/文件本地缓存,减少CDN流量消耗

    写在前面 小程序网络图片读取: 在读取OSS图片CDN分发时流量大量消耗,导致资金费用增加. 网络图片比较大时,图片加载缓慢. 为了尽量减少上面两个问题,所以对已读的图片进行缓存处理,减少多次访问不必 ...

  8. less文件编译成微信小程序wxss文件

    2016年9月21日,微信小程序正式开启内测.在微信生态下,触手可及.用完即走的微信小程序引起广泛关注,刷爆朋友圈子.在这样的火爆氛围中,作为一个前端开发者的我,也悄悄地去尝鲜.在做demo小示例的过 ...

  9. python 如何读取大文件

    一般的读取文件的方法: with open(file_path, "r") as f: print f.read() 或者 with open(file_path,"r& ...

随机推荐

  1. 面向科学计算的Python IDE--Anaconda

    1.下载 2.安装,假定路径为D:/Anaconda 3.在命令行中查看已安装的包及其版本 D: cd Anaconda conda list 结果: # packages in environmen ...

  2. C# 3种方法连接MySql

    转   http://wenku.baidu.com/view/d0cf34708e9951e79b8927c7.html C# 连接MYSQL数据库的方法及示例 连接MYSQL数据库的方法及示例 方 ...

  3. 数据结构-浙大 MOOC 笔记一 基本概念

    做一些笔记记录自己的学习过程 第一节课介绍了数据结构的基本概念,首先没有直接给出相关的定义而是通过思考如何在书架上摆放书籍这样一个简单的类比了解到数据的组织方式的重要性,并通过printN函数的循环实 ...

  4. 将十进制数转为一个n位数的密码(每位都是个m进制数)

    例如一个6位数的10进制密码,共有106个密码,如果把每个6位数的密码编成号就是[0,106-1].这是十进制的情况,即6个位,每个位有10种选择.如果要遍历所有密码,需要6重for循环,每个循环10 ...

  5. erlang 虚机性能调优

    erlang 默认启动参数更多的是针对电信平台实时特性,简单调整参数能很大程度降低CPU消耗,提高处理能力. 1. 关闭spin_wait 设置参数:+sbwt none 我上一篇文章提到:erlan ...

  6. CI 扩展 Service

    CI 扩展 Service 说明 CodeIgniter是一套典型的MVC框架,M负责数据,C负责交互,V负责视图,但是随着业务逻辑越来越复杂, 必然会涉及到一些列操作过程,例如用户下订单,就会存在校 ...

  7. 获取文本的编码类型(from logparse)

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  8. Android安全攻防战,反编译与混淆技术完全解析(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/49738023 之前一直有犹豫过要不要写这篇文章,毕竟去反编译人家的程序并不是什么值 ...

  9. xcode 8 重新支持插件

    苹果出了Xcode8之后,就加了签名让之前的自定义插件无法继续的安装使用.想要重新使用插件的话只要用自己的签名覆盖苹果的签名即可. 1.创建自签名证书 钥匙串->钥匙串访问->证书助理-& ...

  10. asp.net 读取导入的project(mpp)文件

    公司项目有用到读取project文件(.mpp)并保存到指定数据库类似的功能. 查了一下大家总结的方法. 找到一哥们代码,初步判断可行,特此收藏. using System.IO; using Mic ...