日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

使用python实现大文件分割与合并

發(fā)布時間:2025/3/20 python 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用python实现大文件分割与合并 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

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

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

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

在學習python之后,我們自己就可以解決這個問題啦。

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

''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' import sys,oskilobytes = 1024 megabytes = kilobytes*1000 chunksize = int(200*megabytes)#default chunksizedef split(fromfile,todir,chunksize=chunksize):if not os.path.exists(todir):#check whether todir exists or notos.mkdir(todir) else:for fname in os.listdir(todir):os.remove(os.path.join(todir,fname))partnum = 0inputfile = open(fromfile,'rb')#open the fromfilewhile True:chunk = inputfile.read(chunksize)if not chunk: #check the chunk is emptybreakpartnum += 1filename = os.path.join(todir,('part%04d'%partnum))fileobj = open(filename,'wb')#make partfilefileobj.write(chunk) #write data into partfilefileobj.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大小,我們現(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 >>>

這是分割后的文件:

下面是文件合并腳本:

''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' import sys,osdef 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 directoryfiles.sort() #sort part files to read in orderfor 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实现大文件分割与合并的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。