国产成人AV无码一二三区,少女1到100集,国产精品久久久久精品综合紧,巜公妇之诱感肉欲HD在线播放

文章 > 头条 > 如何用Python实现分割合并文件

如何用Python实现分割合并文件

头像

爱喝马黛茶的安东尼

2019-12-13 17:54:512489浏览 · 0收藏 · 0评论

在平常的生活中,我们会遇到下面这样的情况:

你下载了一个比较大型的游戏(假设有10G),现在想跟你的同学一起玩,你需要把这个游戏拷贝给他。

然后现在有一个问题是文件太大(我们不考虑你有移动硬盘什么的情况),假设现在只有一个2G或4G的优盘,该怎么办呢?

有很多方法,例如winrar压缩的时候分成很多小卷,这里不累述。

在学习python之后,我们自己就可以解决这个问题啦。

我们可以自己写一个脚本去分割合并文件,将文件分割成适合优盘大小的小文件,再拷贝,然后再合并。

import sys,os
kilobytes = 1024
megabytes = kilobytes*1000
chunksize = int(200*megabytes)#default chunksize
def split(fromfile,todir,chunksize=chunksize):
    if not os.path.exists(todir):#check whether todir exists or not
        os.mkdir(todir)          
    else:
        for fname in os.listdir(todir):
            os.remove(os.path.join(todir,fname))
    partnum = 0
    inputfile = open(fromfile,'rb')#open the fromfile
    while True:
        chunk = inputfile.read(chunksize)
        if not chunk:             #check the chunk is empty
            break
        partnum += 1
        filename = os.path.join(todir,('part%04d'%partnum))
        fileobj = open(filename,'wb')#make partfile
        fileobj.write(chunk)         #write data into partfile
        fileobj.close()
    return partnum
if __name__=='__main__':
        fromfile  = input('File to be split?')
        todir     = input('Directory to store part files?')
        chunksize = int(input('Chunksize to be split?'))
        absfrom,absto = map(os.path.abspath,[fromfile,todir])
        print('Splitting',absfrom,'to',absto,'by',chunksize)
        try:
            parts = split(fromfile,todir,chunksize)
        except:
            print('Error during split:')
            print(sys.exc_info()[0],sys.exc_info()[1])
        else:
            print('split finished:',parts,'parts are in',absto)

下面是脚本运行的例子:

我们在F有一个X—MEN1.rar文件,1.26G大小,我们现在把它分割成400000000bit(大约380M)的文件。

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
File to be split?F:\X-MEN1.rar
Directory to store part files?F:\split
Chunksize to be split?400000000
Splitting F:\X-MEN1.rar to F:\split by 400000000
split finished: 4 parts are in F:\split
>>>

这是分割后的文件:

1a1c285415c97d6a5182568e87ffed2.png

下面是文件合并脚本:

import sys,os
def joinfile(fromdir,filename,todir):
    if not os.path.exists(todir):
        os.mkdir(todir)
    if not os.path.exists(fromdir):
        print('Wrong directory')
    outfile = open(os.path.join(todir,filename),'wb')
    files = os.listdir(fromdir) #list all the part files in the directory
    files.sort()                #sort part files to read in order
    for file in files:
        filepath = os.path.join(fromdir,file)
        infile = open(filepath,'rb')
        data = infile.read()
        outfile.write(data)
        infile.close()
    outfile.close()
if __name__=='__main__':
        fromdir = input('Directory containing part files?')
        filename = input('Name of file to be recreated?')
        todir   = input('Directory to store recreated file?')
        
        try:
            joinfile(fromdir,filename,todir)
        except:
            print('Error joining files:')
            print(sys.exc_info()[0],sys.exc_info()[1])

运行合并脚本,将上面分割脚本分割的文件重组:

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Directory containing part files?F:\split
Name of file to be recreated?xman1.rar
Directory to store recreated file?F:\
>>>

运行之后可以看到F盘下生成了重组的xman.rar。

python学习网,免费的在线学习python平台,欢迎关注!

本文转自:https://www.jianshu.com/p/587e99c494f5

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

《破外女》高清完整版| 女性扒开大腿内侧小肚子痒| 录像狗配女人| 《疯狂伴娘》在线观看| 欧美片《肉欲进入》| 《酒店激战》动漫1-5集免费观.| 《尼姑春心荡漾》大结局| 法国《口咬》电影完整版| 国产免费观看高清电视剧| 看动漫| 无法抗拒的你ID中字| 《初次深交流》韩剧| 小蜜桃免费观看高清电视剧| 两个奶头被吃得又翘又肿特别疼 | 精品久久免费一区二区三区四区| 禁忌:家庭密码| 榨精病栋| 浪妇荡SAO嗯啊~轻点H漫画| 美景之屋2电视剧高清完整版在线..| 公交车上被蹭水后多久自然恢复| 丰满的女教师HD中字3D| CSGO高清大片免费播放器| 家族轮换之共乐的日子最新章节更,| 免费观看已满十八岁的电视剧大全| 《交换温柔》免费观看全集完整版| 6996电视影片免费人数202..| 父女一起到达巅峰的小说| 黑科网 今日黑科独家爆料| 公日日躁我和公乱小说| 苏软软汆肉的日常 花卷| BOBO浏览器| 无人区免费观看高清电视剧| 7777777亚洲和欧洲尺码表| 灭火宝贝2户外版| 公交车上被蹭水后多久自然恢复| 扌暴辶鬲和扌暴辶的区别| 爱动漫| 兄妹客厅当父母面爱叫啥| 四十九岁老阿姨燥热入睡困难的原.| 二人努力生猴子免费观看| 《黏糊糊的你》动漫免费观看