python 小程序大文件的拆分合并
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 小程序大文件的拆分合并的更多相关文章
- Python逐块读取大文件行数的代码 - 为程序员服务
Python逐块读取大文件行数的代码 - 为程序员服务 python数文件行数最简单的方法是使用enumerate方法,但是如果文件很大的话,这个方法就有点慢了,我们可以逐块的读取文件的内容,然后按块 ...
- Python 小程序,对文件操作及其它
以下是自己写的几个对文件操作的小程序,里面涉及到文件操作,列表(集合,字典)的运用等.比方说,从文件里读取一行数据.分别存放于列表中,再对列表进行操作.如去掉里面的反复项.排序等操作. 常见对文件里行 ...
- 怎么样通过编写Python小程序来统计测试脚本的关键字
怎么样通过编写Python小程序来统计测试脚本的关键字 通常自动化测试项目到了一定的程序,编写的测试代码自然就会很多,如果很早已经编写的测试脚本现在某些基础函数.业务函数需要修改,那么势必要找出那些引 ...
- Day1:第一个python小程序
Day1:第一个python小程序与开发工具Pycharm 一.Hello World C:\Users\wenxh>python Python 3.6.2 (v3.6.2:5fd33b5, J ...
- python处理分隔大文件
4个.sql格式的文件,2G大小,直接插入mysql数据中,文件太大了,导入不进去. 太大的文件用python处理也很麻烦,处理不了,只能先分隔成小文件处理. 文件中数据格式:其中values里面的数 ...
- 一个有意思的Python小程序(全国省会名称随机出题)
本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 最近比较迷Python,仿照<Python编程快速上手>8.5写了一个随机出卷的小 ...
- 小程序-图片/文件本地缓存,减少CDN流量消耗
写在前面 小程序网络图片读取: 在读取OSS图片CDN分发时流量大量消耗,导致资金费用增加. 网络图片比较大时,图片加载缓慢. 为了尽量减少上面两个问题,所以对已读的图片进行缓存处理,减少多次访问不必 ...
- less文件编译成微信小程序wxss文件
2016年9月21日,微信小程序正式开启内测.在微信生态下,触手可及.用完即走的微信小程序引起广泛关注,刷爆朋友圈子.在这样的火爆氛围中,作为一个前端开发者的我,也悄悄地去尝鲜.在做demo小示例的过 ...
- python 如何读取大文件
一般的读取文件的方法: with open(file_path, "r") as f: print f.read() 或者 with open(file_path,"r& ...
随机推荐
- JavaScript原型
prototype与_proto_ 对象的 prototype 属性的方法.属性为对象所属的那一"类"所共有.对象原型链通过 proto 属性向上寻找. 为 proto 指定 nu ...
- clearfix的应用
之前遇到一个问题,引用Bootstrap框架时 一行显示四个模块,小屏幕时显示两个模块 当内容一样时,大小屏幕时一样的,但是当其中一个和另一个内容不同时,展示效果就会有错乱 <div class ...
- iOS9,导航控制器中的子控制器设置StatusBar状态失效的问题
iOS9之前控制StatusBar的两种方式: 第一种方式:全局控制StatusBar 1. 在项目的Info.plist文件里设置UIViewControllerBasedStatusBarAppe ...
- thinkphp判断是否登录
自己写一个BasicController继承了官方的Controller,将判断登录的代码放在BasicController中,然后让其他自己编写的Controller都继承BasicControll ...
- UEFI模式下Win10和Linux双系统
一.准备 用Win自带的磁盘管理或者进PE分出一块空间来. 你必须要有一个U盘,然后使用软碟通或者ImageWriter把iso系统镜像文件烧录进去,这是比较传统的方法,但既然我们UEFI启动,那就根 ...
- bootstrap validate 实现页面动态验证(formvalidate)
关于基本的bootstrap validate 验证方法外面有许多博客上都有讲解,我就不在过多叙述了.大家也可以去看官网api:http://bv.doc.javake.cn/api/ 今天要说的是动 ...
- C# WinForm实现Windows 7 Aero磨砂玻璃效果
在Vista系统之后,微软为窗体程序提供了Aero磨砂的效果,如下图.那么用C#如 何来实现这种磨砂效果呢? 代码: using System; using System.Collections.Ge ...
- How to see the "real" available resources ?
Hi, Hope this will help you : nova hypervisor-stats It will return the statistics of the Hypervisor ...
- 关于.NET知识体系结构图总结
转载:关于.NET知识体系结构图总结-零度http://www.xcode.me/book/net-framework-maps 最近对.NET框架方面的知识进行了概要的总结,整理了一些知识体系结构图 ...
- POI完美解析Excel数据到对象集合中(可用于将EXCEL数据导入到数据库)
实现思路: 1.获取WorkBook对象,在这里使用WorkbookFactory.create(is); // 这种方式解析Excel.2003/2007/2010都没问题: 2.对行数据进行解析 ...