[Tutor] Splitting a file

Antonio Rodriguez taserian at gmail.com
Sun Dec 17 13:52:54 CET 2006


I'm attempting to split a binary file into 5 component files, as a
"challenge". I thought that the following would work:

f = open('/home/taser/Desktop/inputfile.bin')
f1 = []
f2 = []
f3 = []
f4 = []
f5 = []
while 1:
        try:
                bytes = list(f.read(5))
                f1.append(bytes[0])
                f2.append(bytes[1])
                f3.append(bytes[2])
                f4.append(bytes[3])
                f5.append(bytes[4])
        except:
                break
ff1 = open('/home/taser/Desktop/ff1.file','w')
ff1.write(''.join(f1))
ff1.close()

I've only dealt with one of the lists, since I want to test it before
doing the rest.

I'm unsure if I'm going about this correctly, since I'm manipulating
binary data as if it were a text file.

Also, I'm told that I should be able to have Python itself determine the
type of file it is, since the file contains a "magic number". However, I
haven't had any luck in my searches for how to do that in Python.

Any help would be appreciated.





More information about the Tutor mailing list