In the ordinary life, we will encounter the following situation:
You downloaded a larger game (assuming 10G) and now want to play with your classmates, you need to copy this game to him.
Then there is a problem is the file is too large (we do not consider what you have to move the hard disk case), assuming now only a 2G or 4G USB flash drive, what to do?
There are many ways, such as WinRAR compression when divided into a lot of small volumes, here is not exhausted.
After learning Python, we can solve this problem ourselves.
We can write a script ourselves to split the merged files, split the files into small files suitable for the size of the USB stick, copy them, and then merge them.
Here is the file splitting script:
1 ImportSys,os2 3kilobytes = 10244megabytes = kilobytes*10005chunksize = Int (200*megabytes)#default Chunksize6 7 defSplit (fromfile,todir,chunksize=chunksize):8 if notOs.path.exists (Todir):#Check whether Todir exists or not9 Os.mkdir (Todir)Ten Else: One forFNameinchOs.listdir (todir): A Os.remove (Os.path.join (todir,fname)) -Partnum =0 -Inputfile = open (FromFile,'RB')#Open the FromFile the whileTrue: -Chunk =Inputfile.read (chunksize) - if notChunk#Check the chunk is empty - Break +Partnum + = 1 -filename = Os.path.join (Todir, ('part%04d'%partnum)) +fileobj = open (filename,'WB')#Make partfile AFileobj.write (Chunk)#write data into Partfile at fileobj.close () - returnPartnum - if __name__=='__main__': -FromFile = input ('File to is split?') -Todir = input ('Directory to store part files?') -chunksize = Int (input ('Chunksize to be split?')) inAbsfrom,absto =map (Os.path.abspath,[fromfile,todir]) - Print('splitting', Absfrom,' to', Absto,' by', chunksize) to Try: +Parts =Split (fromfile,todir,chunksize) - except: the Print('Error during split:') * Print(Sys.exc_info () [0],sys.exc_info () [1]) $ Else:Panax Notoginseng Print('Split finished:', parts,'Parts is in', Absto)
Here is an example of how the script runs:
We have a x-men1.rar file in F, 1.26G size, we now divide it into 400000000bit (about 380M) of files.
Python 3.4.1 (V3.4.1:C0E311E010FC, May, 10:45:13) [MSC v.1600 64bit (AMD64)] on Win32type"Copyright","credits" or "license ()" forMore information.>>> ================================ RESTART ================================>>>File to is split? F:\x-men1.rardirectory to store part files? F:\splitChunksize to be split?400000000splitting f:\x-men1.rar to F:\split by 400000000Split finished:4 Parts isinchF:\split>>>
This is the file after the split:
Here is the file merge script:
1 ImportSys,os2 3 defJoinfile (fromdir,filename,todir):4 if notos.path.exists (todir):5 Os.mkdir (Todir)6 if notos.path.exists (fromdir):7 Print('Wrong directory')8outfile = open (Os.path.join (todir,filename),'WB')9Files = Os.listdir (Fromdir)#List All the part files in the directoryTenFiles.sort ()#sort part Files to the read in order One forFileinchFiles: Afilepath =Os.path.join (fromdir,file) -infile = open (filepath,'RB') -data =Infile.read () the outfile.write (data) - infile.close () - outfile.close () - if __name__=='__main__': +Fromdir = input ('Directory containing part files?') -filename = input ('Name of file to is recreated?') +Todir = input ('Directory to store recreated file?') A at Try: - joinfile (Fromdir,filename,todir) - except: - Print('Error joining files:') - Print(Sys.exc_info () [0],sys.exc_info () [1])
Run the merge script to reorganize the files divided by the split script above:
Python 3.4.1 (V3.4.1:C0E311E010FC, May, 10:45:13) [MSC v.1600"copyright" " credits " or " license () " for More information. >>> ================================ RESTART ================================>>> Directory Containing part files? F:\splitName of file to is recreated?xman1.rardirectory to store recreated file? F:
After running, you can see the xman.rar generated by the F-disk.
Python Learning-Large file segmentation and merging