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.
Python, 8 lines
1 2 3 4 5 6 7 8
#!/usr/bin/env python import base64 import sys encode_or_decode = 'encode' in sys.argv.lower() and base64.encode or 'decode' in sys.argv.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) or sys.stdin, len(sys.argv) > 2 and open(sys.argv,'w') or sys.stdout)
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.
Thanks Sunjay for brushing it up! I have to keep reminding myself that
white spaceis the main element of design.