Encodes or decodes base64-encoded files. If the input or output file(s) are ommited standard input/output will be used.
The encoding direction is detected from the file name: for decoding save the script e.g. as encode64.py. For decoding create a symlink with decode
in the name.
1 2 3 4 5 6 7 8 | #!/usr/bin/env python
import base64
import sys
encode_or_decode = 'encode' in sys.argv[0].lower() and base64.encode or 'decode' in sys.argv[0].lower() and base64.decode or (sys.stderr.write("Error: script name must contain 'encode' or 'decode'!\n") or sys.exit(-1))
encode_or_decode(len(sys.argv) > 1 and open(sys.argv[1]) or sys.stdin, len(sys.argv) > 2 and open(sys.argv[2],'w') or sys.stdout)
|
Tags: base64
Can you break up the long lines? They aren't wrapping!
Python is a very great language. Putting all your code on 2 or 4 lines really will not make a difference in Python. The only thing it will do is make your code harder to read and edit later on.
This code will be less error prone as well:
With the now longer code, you can also add things like the ability to provide only an output file.
-Sunjay03
Thanks Sunjay for brushing it up! I have to keep reminding myself that
white space
is the main element of design.