溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

用Python實(shí)現(xiàn)分割合并文件的方法

發(fā)布時(shí)間:2020-08-01 13:58:25 來(lái)源:億速云 閱讀:181 作者:清晨 欄目:編程語(yǔ)言

小編給大家分享一下用Python實(shí)現(xiàn)分割合并文件的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

在平常的生活中,我們會(huì)遇到下面這樣的情況:

你下載了一個(gè)比較大型的游戲(假設(shè)有10G),現(xiàn)在想跟你的同學(xué)一起玩,你需要把這個(gè)游戲拷貝給他。

然后現(xiàn)在有一個(gè)問題是文件太大(我們不考慮你有移動(dòng)硬盤什么的情況),假設(shè)現(xiàn)在只有一個(gè)2G或4G的優(yōu)盤,該怎么辦呢?

有很多方法,例如winrar壓縮的時(shí)候分成很多小卷,這里不累述。

在學(xué)習(xí)python之后,我們自己就可以解決這個(gè)問題啦。

我們可以自己寫一個(gè)腳本去分割合并文件,將文件分割成適合優(yōu)盤大小的小文件,再拷貝,然后再合并。

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)

下面是腳本運(yùn)行的例子:

我們?cè)贔有一個(gè)X—MEN1.rar文件,1.26G大小,我們現(xiàn)在把它分割成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
>>>

這是分割后的文件:

用Python實(shí)現(xiàn)分割合并文件的方法

下面是文件合并腳本:

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])

運(yùn)行合并腳本,將上面分割腳本分割的文件重組:

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:\
>>>

運(yùn)行之后可以看到F盤下生成了重組的xman.rar。

看完了這篇文章,相信你對(duì)用Python實(shí)現(xiàn)分割合并文件的方法有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI