Python learning-big file segmentation and merging

Source: Internet
Author: User

Python learning-big file segmentation and merging
In our daily life, we will encounter the following situation: you have downloaded a relatively large game (assuming there are 10 Gb) and want to play with your classmates, you need to copy the game to him. Now there is a problem: the file is too large (we don't consider whether you have a mobile hard disk or something). What should we do if there is only one 2G or 4g usb flash drive? There are many methods, such as dividing winrar into many small volumes during compression, which are not described here. After learning python, we can solve this problem by ourselves. We can write a script to split the merged files, split the files into small files suitable for the ultra disk size, copy the files, and then merge them. The following is a file splitting Script: Copy code 1 import sys, OS 2 3 kilobytes = 1024 4 megabytes = kilobytes * 1000 5 chunksize = int (200 * megabytes) # default chunksize 6 7 def split (fromfile, todir, chunksize = chunksize): 8 if not OS. path. exists (todir): # check whether todir exists or not 9 OS. mkdir (todir) 10 else: 11 for fname in OS. listdir (todir): 12 OS. remove (OS. path. join (todir, fname) 13 partnum = 014 inputfile = open (fromfile, 'rb') # op En the fromfile15 while True: 16 chunk = inputfile. read (chunksize) 17 if not chunk: # check the chunk is empty18 break19 partnum ++ = 120 filename = OS. path. join (todir, ('Part % 04d '% partnum) 21 fileobj = open (filename, 'wb') # make partfile22 fileobj. write (chunk) # write data into partfile23 fileobj. close () 24 return partnum25 if _ name __= = '_ main _': 26 fromfile = input ('file to be split? ') 27 todir = input ('Directory to store part files? ') 28 chunksize = int (input ('chunksize to be split? ') 29 absfrom, absto = map (OS. path. abspath, [fromfile, todir]) 30 print ('splitting', absfrom, 'to', absto, 'by', chunksize) 31 try: 32 parts = split (fromfile, todir, chunksize) 33 bytes T: 34 print ('error during split: ') 35 print (sys. exc_info () [0], sys. exc_info () [1]) 36 else: 37 print ('split finished: ', parts, 'parts are in', absto) The following is an example of script running: we have a x-men1.rar file in f. the size is 1.26G. We split it into 380 bytes (about MB. Copy Python 3.4.1 (v3.4.1: c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32Type "copyright", "credits" or "license () "for more information. >>> ================================== RESTART ======== ============================================>>> File to be split? F: \ X-MEN1.rarDirectory to store part files? F: \ splitChunksize to be split? 400000000 Splitting F: \ X-MEN1.rar to F: \ split by 400000000 split finished: 4 parts are in F: \ split >>> copy the code this is the split file: the following is a file merging Script: copy the Code 1 import sys, OS 2 3 def joinfile (fromdir, filename, todir): 4 if not OS. path. exists (todir): 5 OS. mkdir (todir) 6 if not OS. path. exists (fromdir): 7 print ('wrong directory') 8 outfile = open (OS. path. join (todir, filename), 'wb ') 9 files = OS. listdir (fromdir) # list all the part f Iles in the directory10 files. sort () # sort part files to read in order11 for file in files: 12 filepath = OS. path. join (fromdir, file) 13 infile = open (filepath, 'rb') 14 data = infile. read () 15 outfile. write (data) 16 infile. close () 17 outfile. close () 18 if _ name __= = '_ main _': 19 fromdir = input ('Directory ining part files? ') 20 filename = input ('name of file to be recreated? ') 21 todir = input ('Directory to store recreated file? ') 22 23 try: 24 joinfile (fromdir, filename, todir) 25 bytes T: 26 print ('error joining files:') 27 print (sys. exc_info () [0], sys. exc_info () [1]) copy the code to run the merge script, and reorganize the file separated by the preceding split Script: copy the Python 3.4.1 code (v3.4.1: c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32Type "copyright", "credits" or "license ()" for more information. >>> ================================== RESTART ======== ==================================>> Directory containing part files? F: \ splitName of file to be recreated? Xman1.rarDirectory to store recreated file? F :\>>>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.