Skip to content

Instantly share code, notes, and snippets.

@rwest
Created November 17, 2009 19:38
Show Gist options
  • Save rwest/237198 to your computer and use it in GitHub Desktop.
Save rwest/237198 to your computer and use it in GitHub Desktop.
Recursively search folders for '.log' files and scan them for a certain regular expression.
#! /usr/bin/env python
# Recursively search folders for '.log' files and scan them for a certain regular expression.
import re # regular expressions
import fileinput, sys, os
# precompile the search into a RegularExpression object to make it faster when we do it lots of times
searchExpression=re.compile("Sum\ of\ electronic\ and\ zero-point\ Energies=\s*([\-0-9.]+)")
def process_folder(folderpath):
#print "searching",folderpath
folders_to_process=[]
files_to_process=[]
for entry in os.listdir(folderpath):
path = os.path.join(folderpath, entry)
if os.path.isdir(path):
folders_to_process.append(path)
elif re.search('\.log$',entry):
files_to_process.append(path)
if files_to_process:
process_files(files_to_process)
for path in folders_to_process:
process_folder(path)
def process_files(filelist):
# fileinput takes all command line arguments (sys.argv[1:]) as files
# or if given a list of files, uses those
for line in fileinput.input(filelist):
match=searchExpression.search(line)
if match:
(fileroot,filextension) = os.path.splitext(fileinput.filename())
(filepath,filename) = os.path.split(fileroot)
print "%s\t%s\t%s" % (filepath,filename , match.groups()[0])
# process the folder given on the command line
if sys.argv[1:]:
for path in sys.argv[1:]:
if os.path.isdir(path):
process_folder(path)
else:
print "Please supply folder(s) to search. (folders, not files)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment