Last active
August 29, 2015 14:04
-
-
Save zvodd/b9648087c2287c7d0126 to your computer and use it in GitHub Desktop.
Simple Example Command Line Python App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
def main(): | |
if len(sys.argv) >= 2: | |
infile = sys.argv[1] | |
else: | |
infile = sys.stdin | |
if len(sys.argv) >= 3: | |
outfile = sys.argv[2] | |
else: | |
outfile = sys.stdout | |
try: | |
if type(infile) == file: | |
infh = infile | |
else: | |
infh = open(infile,'r') | |
data = infh.read() | |
infh.close() | |
except IOError: | |
exit("Failed to open file for %r reading" % infile) | |
try: | |
if type(outfile) == file: | |
outfh = outfile | |
else: | |
outfh = open(outfile,'w') | |
for count,char in enumerate(data): | |
seperator = ' ' | |
if count % 8 == 7 : | |
seperator = '\n' | |
outfh.write("{0:08b}{1}".format(ord(char), seperator)) | |
outfh.close() | |
except IOError: | |
exit("Failed to open file for %r writing" % outfile) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment