首页 技术 正文
技术 2022年11月6日
0 收藏 638 点赞 564 浏览 2265 个字

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

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

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

#! usr/bin/python
# -*- coding:utf-8 -*-import sys, osmegebytes = 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 partnumdef 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 osreadsize = 1024def 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模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,110
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,202
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,837
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,920