Created
October 3, 2012 23:45
-
-
Save chadwhitacre/3830608 to your computer and use it in GitHub Desktop.
Script for Saxifrage Class to Chew On
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 | |
# The "import" statement finds modules (whatever those are!) and defines names | |
# in a namespace (whatever that is!): | |
# | |
# http://docs.python.org/reference/simple_stmts.html#the-import-statement | |
# | |
# The "sys" module provides things related (apparently) to the interpreter | |
# (whatever that is!): | |
# | |
# http://docs.python.org/library/sys.html | |
import sys | |
USAGE = "Usage: ./find.py word filename [filename]*" | |
def fail(msg): | |
print >> sys.stderr, USAGE | |
print >> sys.stderr, msg | |
sys.exit() | |
try: | |
word = sys.argv[1] | |
except: | |
fail("Please provide a word to find.") | |
filenames = sys.argv[2:] | |
if not filenames: | |
fail("Please enter at least one filename.") | |
for filename in filenames: | |
try: | |
file_pointer = open(filename) | |
except IOError: | |
print >> sys.stderr, "Could not open file:", filename | |
continue | |
for line in file_pointer: | |
if word in line: | |
print filename | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment