read text file byte by byte

daved170 daved170 at gmail.com
Sun Dec 13 01:15:50 EST 2009


On Dec 13, 2:34 am, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Sat, 12 Dec 2009 10:46:01 +0100, census <cen... at no-email.de>
> declaimed the following in gmane.comp.python.general:
>
>
>
> > def scramble (a): return (a + 13) % 256
>
>         I'll see your modulo rot 13 and raise with a exclusive or...
>
> -=-=-=-=-
>
> import sys
>
> def scramble(block, key="don't look"):
>     copies = int(len(block) / len(key)) + 1
>     keystring = key * copies
>     return "".join([ chr( ord(block[i])
>                           ^ ord(keystring[i]))
>                      for i in range(len(block))])
>
> def process(fin, fout, key=None):
>     din = open(fin, "rb")
>     dout = open(fout, "wb")
>     while True:
>         block = din.read(1024)
>         if not block: break
>         if key is None:
>             block = scramble(block)
>         else:
>             block = scramble(block, key)
>         dout.write(block)
>     dout.close()
>     din.close()
>
> if __name__ == "__main__":
>     fin = sys.argv[1]
>     fout = sys.argv[2]
>     if len(sys.argv) > 3:
>         key = sys.argv[3]
>     else:
>         key = None
>     process(fin, fout, key)
> --
>         Wulfraed         Dennis Lee Bieber               KD6MOG
>         wlfr... at ix.netcom.com      HTTP://wlfraed.home.netcom.com/


Thank you all.
Dennis I really liked you solution for the issue but I have two
question about it:
1) My origin file is Text file and not binary
2) I need to read each time 1 byte. I didn't see that on your example
code.
Thanks again All of you
Dave



More information about the Python-list mailing list