#!/usr/bin/python
# Demo code. Decode base64 content from a string or a readable file
# Works for Python 2.7.3, at least
# Usage
# decodebase64.py base64string
# decodebase64.py /absolute/path/to/base64/file
import sys
import os
import base64
# testStr is for test usage, when they are assinged to the variable s to replace ''.
#testStr='Y29uZmlnLmpzb24ub2Jmc3Byb3h5LjIwMTYwMjIyXzE3NDcyOA=='
#testStr2='/path/to/base64/file'
def decodeStr(s):
rslt=base64.b64decode(s)
print 'Decoded result:\n',rslt
def decodeFile(fPath):
f = open(fPath, 'r') # better to use the with-as clause
readed = [line.rstrip() for line in f]
f.close()
strReaded = ''.join(readed)
print strReaded
decodeStr(strReaded)
def main():
print 'Args: ',str(sys.argv)
# s = testStr
s = ''
if len(sys.argv) > 1:
s=sys.argv[1]
if os.path.isabs(s):
decodeFile(s)
elif len(s) > 0:
decodeStr(s)
else:
print "Nothing I can do"
main()