Thursday, September 13, 2012

Basic grep: find the lines in a file containing the given string

#!/usr/bin/python

import os

sourceFile = raw_input("Please enter the name of the file to search in: ")

if (os.path.exists(sourceFile)):
    pass
else:

    print "Sorry, cant find the file! Please check if the filename and path are

correct!\n"
    exit(0)

text = raw_input("Please enter the string to look for: ")

file = open(sourceFile,"r")

for line in file.readlines():
    if text in line:
        print line
   
file.close()

No comments:

Post a Comment