Skip to content

Instantly share code, notes, and snippets.

@MattJermyWright
Created July 25, 2013 04:23
Show Gist options
  • Save MattJermyWright/6076894 to your computer and use it in GitHub Desktop.
Save MattJermyWright/6076894 to your computer and use it in GitHub Desktop.
Basic Python File Reader Skeleton
#!/usr/bin/env python
import re
import sys
from optparse import OptionParser
def main():
commandLine = OptionParser()
commandLine.add_option("-f", "--file", type="string", dest="filename",
help="Read from specific FILE", action="store", metavar="FILE")
(options, args) = commandLine.parse_args()
fileToOpen = sys.stdin
if options.filename:
fileToOpen = open(options.filename, "r")
processLines(fileToOpen)
def processLines(fileHandle):
cleanRe = re.compile(r'\s+')
for line in fileHandle:
line = cleanRe.sub(' ', line.strip(' \t\n\r'))
print "cleanLine: " + line
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment