writing to a binary file without intervening spaces

Paul Rubin http
Mon Mar 17 04:28:55 EDT 2008


Larry <larry.cebuala at gmail.com> writes:
> My data is a = [23, 45, 56, 255].
> My desire output is: 234556255, of course in binary file
> representation already.
> 
> I tried to make one using write after pack method of struct module,
> but because of spaces I got incorrect results.

struct.pack works for me:

    Python 2.4.4 (#1, Oct 23 2006, 13:58:00) 
    >>> import struct, os
    >>> x = struct.pack('BBBB', 23, 45, 56, 255)
    >>> len(x)
    4
    >>> f = open('/tmp/foo','w'); f.write(x); f.close()
    >>> os.system("ls -l /tmp/foo")
    -rw------- 1 phr phr 4 Mar 17 01:27 /tmp/foo
    >>> 

You could also do the list-string conversion with the array module.



More information about the Python-list mailing list