'''Find sample code in my python directory. zzz.nbb.20110621.findstring.in.one.file.01.py adapt to select one python file, and find a given string zzz.nbb.20110621.findcode.01.py adapt for the 20110621 directory nbb.findcode.01.py 3/29/2010 N. Bliss ''' ##import os, sys, traceback, time, glob import os, sys, traceback, time import ssurgo_m002_timer as timer def timer_f(message): global previoustime total = time.clock() step = total - previoustime print 'Timer: %s: step %15.5f, elapsed %15.5f' % ( message, step, total) previoustime = total ######################### ##### __main__ ########################################## if __name__ == '__main__': try: starttime = time.clock() previoustime = starttime print 'Start ',time.ctime(), ' ' * 5, os.path.abspath(sys.argv[0]) # E.g., 'Start Sat Jun 27 11:09:37 2009 D:\Wylie.Owyhee\PythonScripts\ssurgo.extract.06.py' previoustime = timer.timer_f('Initial', starttime, previoustime) timestamp = '%4d%02d%02d%02d%02d%02d' % time.localtime()[:6] # string with year, month, day, hour, minute second: '20090204124750' filename = raw_input('Enter the full path filename: ') if not filename: print 'No string found, stopping...' raise findstring = raw_input('Enter the findstring: ') if not findstring: print 'No string found, stopping...' raise printlimit = raw_input('Enter integer for number of lines to print (default = 10): ') if not printlimit: print 'No print limit found, default to 10' printlimit2 = 10 else: try: printlimit2 = int(printlimit) except: print 'The print limit must be a number, stopping...' raise matchcase = raw_input('Match case? (No, Yes): ') if not matchcase: print 'Nothing entered for Match Case, default to No...' matchcase_bool = False else: if matchcase.lower() == 'yes' or matchcase.lower() == 'y': matchcase_bool = True elif matchcase.lower() == 'no' or matchcase.lower() == 'n': matchcase_bool = False else: print 'Match Case reply must be "Yes" or "No", stopping...' raise ## python_directory = r'D:\SSURGO\20110621\PythonScripts' # 10/13/2011 ## prev_file = "#####" ## ## file_list = glob.glob( os.path.join( python_directory, '*.py') ) ## file_list.sort() ## ## for file1 in file_list: file1 = filename if os.path.exists( file1 ): fi1a = open( file1 ) printcount = 0 file_lines = fi1a.readlines() count_line = 0 for line in file_lines: count_line += 1 if matchcase_bool: # matching case is requested, line should match exactly if line.find(findstring) > -1: if file1 != prev_file: print file1 prev_file = file1 if printcount < printlimit2: printcount += 1 print '%10d %s' % ( count_line, line.rstrip() ) else: break else: # case need not match exactly line2 = line.lower() findstring2 = findstring.lower() if line2.find(findstring2) > -1: if file1 != prev_file: print file1 prev_file = file1 if printcount < printlimit2: printcount += 1 print '%10d %s' % ( count_line, line.rstrip() ) else: break fi1a.close() else: print 'File %s not found' % file1 print 'Finish',time.ctime(), ' ' * 5, os.path.abspath(sys.argv[0]) # E.g., 'Finish Sat Jun 27 11:09:37 2009 D:\Wylie.Owyhee\PythonScripts\ssurgo.extract.06.py' except Exception, msg: print traceback.print_exc() print msg print 'ERROR #####: Finish',time.ctime(), ' ' * 5, os.path.abspath(sys.argv[0]) # E.g., 'Finish Sat Jun 27 11:09:37 2009 D:\Wylie.Owyhee\PythonScripts\ssurgo.extract.06.py'