#!/usr/bin/python # Name: findTextBlock.py # Version: 0.2 # Created: 2018-02-03 # Last modified: 2018-02-03 # Usage: findTextBlock.py start_text end_text filename # Description: The script will display all of the text found in filename # from start_text to end_text, including start_text and end_text. If less # than 3 arguments are provided on the command line, the script will prompt # for the values for start_text, end_text, and filename. A text search string # that includes a space or spaces should be enclosed in double quotes if # provided on the command line. If a double quote is part of the search text, # precede it with the backslash escape character on the command line. import os.path, sys def parse_file(infile,start_text,end_text): with open(infile) as f: alltxt = f.read() found_position = alltxt.find(start_text) if found_position == -1: print "Starting text", start_text, "not found in", infile exit() # Need to eliminate all text before the starting point; otherwise, # if the end text occurs somewhere before the star text, nothing will # be printed even though there may be another occurrence of end text # later in the file. alltxt = alltxt[alltxt.find(start_text):] found_position = alltxt.find(end_text) if found_position == -1: print "Ending text", end_text, "not found after", start_text, "in", infile exit() return alltxt[alltxt.find(start_text):alltxt.find(end_text)+len(end_text)] if len(sys.argv) == 4: start_text = sys.argv[1] end_text = sys.argv[2] path_to_file = sys.argv[3] else: start_text = raw_input("Enter starting text: ") end_text = raw_input("Enter ending text: ") path_to_file = raw_input("Enter file name: ") if not os.path.isfile(path_to_file): exit("Input file not accessible") print parse_file(path_to_file,start_text,end_text)