Connecting Tech Pros Worldwide Forums | Help | Site Map

Split binary file...

Newbie
 
Join Date: May 2009
Posts: 5
#1: May 20 '09
Is there some simple way to split a big binary file (over 500 MBs) into the smaller parts, and after join them back to primordial original file?

bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#2: May 20 '09

re: Split binary file...


I split a JPEG file and successfully joined the parts with the following code:
Expand|Select|Wrap|Line Numbers
  1. fn = 'image.jpg'
  2.  
  3. # Split into 4 files
  4. f = open(fn, 'rb')
  5. data = f.read()
  6. f.close()
  7.  
  8. bytes = len(data)
  9. inc = (bytes+4)/4
  10. fileNames = []
  11. for i in range(0, bytes+1, inc):
  12.     fn1 = "file%s" % i
  13.     fileNames.append(fn1)
  14.     f = open(fn1, 'wb')
  15.     f.write(data[i:i+inc])
  16.     f.close()
  17.  
  18. new_file = 'image_joined.jpg'
  19. dataList = []
  20.  
  21. for fn in fileNames:
  22.     f = open(fn, 'rb')
  23.     dataList.append(f.read())
  24.     f.close()
  25.  
  26. f = open(new_file, 'wb')
  27. for data in dataList:
  28.     f.write(data)
  29. f.close()
Newbie
 
Join Date: May 2009
Posts: 5
#3: May 21 '09

re: Split binary file...


Thanks very much, @bvdet, I will try this code...
Newbie
 
Join Date: May 2009
Posts: 5
#4: May 21 '09

re: Split binary file...


Very useful code, works for me (even on the large files), fast and simple!
Thanks one more time...

King regards,
Stole
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#5: May 21 '09

re: Split binary file...


You are welcome! I am glad it works for you.

-BV
Reply