Binary File Editor

Bernhard Herzog herzog at online.de
Thu Aug 17 10:49:28 EDT 2000


garbett at my-deja.com writes:

> In article <8nemvc$d1j$1 at nnrp1.deja.com>,
>   garbett at my-deja.com wrote:
> > I want to make an editor (in python!) that edits binary files. Most of
> > the data is long/string/long then a big sequence of raw bytes. The raw
> > bytes I'm not interested in editing, but maybe switching for a
> different
> > set of raw bytes. The files are actually a proprietary collection of
> wav
> > files with additional info.
> >
> 
> I've not gotten any response, so I've started trying to write the parser
> in python and remember why I got stuck the first time I tried.
> 
> The first four bytes of the file are a string denoting type, the next
> four is an integer.

Sounds pretty much like RIFF, especially since the files contain wav
files. FWIW, Sketch's CMX import filter contains some RIFF parsing code.
The CMX specific part is pretty ugly but the RIFF code is quite modular.
OTOH, it's not really all that difficult to write your own version and
in Sketch it's just abut 40 lines anyway.

> Quite simply, how do I read the first four bytes into a string and the
> next four into an integer in python? If I can do this I think the rest
> is downhill.
> 
> The FileType supports a read, i.e. 'f.read([n])  Reads at most n bytes'
> but how could this be used to read into python native types???

With the struct module, for instance:

    import struct
    #...
    data = file.read(8)
    chunk_type, length = struct.unpack('<4si', data)


-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list