import sys
import os.path
def FindText():
argc = len( sys.argv )
if argc < 3 :
Usage()
text = sys.argv[ 1 ]
files = sys.argv[ 2: ]
# print ' text: %s\nfiles: %s' % ( text, str( files ) )
for filename in files :
try :
fh = open( filename )
data = fh.read()
fh.close()
if data.find( text ) >= 0 :
print filename, text
except :
sys.stderr.write( 'File not found: ' + filename + '\n' )
def Usage():
print '''Command: FindText.py\n
Role: To search text files for a specific text string\n
Usage: python FindText.py textString fileName [fileName...]\n
Example: python FindText.py ERROR a.txt b.txt c.txt'''
sys.exit()
if ( __name__ == '__main__' ) :
FindText()
else :
Usage()
import sys
import os
def FindText():
files = os.listdir( os.getcwd() )
files.sort()
# print ' text: %s\nfiles: %s' % ( text, str( files ) )
for filename in files :
if filename[ -4: ] == '.txt' :
try :
f = open( filename )
data = f.read()
f.close()
if data.find( 'ERROR' ) >= 0 :
print filename, 'ERROR'
except :
pass
if ( __name__ == '__main__' ) :
FindText()