Binary file splitting

Questions about programming languages and debugging
Post Reply
User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Binary file splitting

Post by l0ngb1t »

been working on a small project for a university course where i needed to split a binary file into blocks for some reasons, i could have use DSplit but i thought why not take advantage of it to practice some python and add something cool to the project paper.

here is the code, it prompts for the file name and the size of the block you want, then it split it for you.

Code: Select all

import os

file_name        = raw_input("enter the full file name  --> ")
block_size       = int(raw_input("enter the block size in bytes  --> "))
statinfo         = os.stat(file_name)
number_of_blocks = statinfo.st_size/block_size
left_over        = statinfo.st_size%block_size
fp               = open(file_name,"rb")

for i in range(0,number_of_blocks):
    block   = fp.read(block_size)
    binfile = open("bin.data."+str(i),"wb")
    binfile.write(block)
    binfile.close()

if left_over:
    block = fp.read(left_over)
    binfile = open("bin.data."+str(number_of_blocks),"wb")
    binfile.write(block)
    binfile.close()

fp.close()
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: Binary file splitting

Post by bad_brain »

well done! and clean well structured code! =D>
Image

Post Reply