hello, I want to change n bytes of a binary file
Fredrik Lundh
fredrik at pythonware.com
Tue Nov 1 12:30:52 EST 2005
"could ildg" wrote:
> > so how can I do the binary stuff?
>
> 8-bit strings contain bytes.
>
> > I want a encrypt function like below:
> > def encrypt(filename,n):
>
> f = open(filename,"rb+")
>
> > a=f.read(n)
>
> b = encrypt(a)
^^^^^^^^Thank you~~,but where is the encrypt defined?
oh, I though you asked how to update the first n bytes of a
binary file.
if you want to XOR the bytes, you can do something like:
import array
b = array.array("B", a)
for i in range(len(b)):
b[i] = b[i] ^ 255
b = b.tostring()
or
b = "".join([chr(ord(c) ^ 255) for c in a])
or some variation thereof.
</F>
More information about the Python-list
mailing list