'''A program created by Travis Mullaly (A.K.A. _ryuurei) to search through the
files/directories in and following a specified directory path for a list of
user-defined keywords. Works in file names as well as file content.
!!! WARNING: Searching through file-content can be dangerous as it requires a
great amount of power. It is recommended that this feature only be used in
directories that contain very few files and sub-directories.
Completed on: 29, June 2010.
Creator: Travis Mullaly
Contact: travis_m.cpp@live.com
'''
import os
import sys
from time import sleep
def perror(location):
'''A function to print a text-based error message.'''
print '-'*83
print ' Access to {0} not allowed. '.format(location).center(83, '#')
print '-'*83
sleep(3)
def scan_dirs(location, queries, action='N', limit=False):
'''A function to search through file names/content.'''
try: files = os.listdir(os.curdir)
except:
perror(location)
print '\nProgram could not continue at this point, and will now close.\n'
sys.exit()
newLocation = location
# If a search in filenames or both was instructed, compare the search
# queries with the names of the files.
if action in ['N', 'n', 'B', 'b']:
for item in files:
for search in queries:
if search in item.lower() and not item.endswith('~')\
and not os.path.isdir(item):
print '\nQuery found:\t{0}'.format(search)
print 'In filename:\t{0}'.format(item)
print 'In directory . . .\n{0}\n'.format(newLocation)
# If a search in file content or both was instructed, compare the
# queries with the content of files.
if action in ['C', 'c', 'B', 'b']:
for item in files:
if not os.path.isdir(item):
for search in queries:
if search in open(item, 'r').read():
print '\nQuery found:\t{0}'.format(search)
print 'In content of:\t{0}'.format(item)
print 'In directory . . .\n{0}\n'.format(newLocation)
# If directed to delete files, search through files (avoiding folders)
# and delete them if any of the quiries specified were found in the name.
if action == 'D' or action == 'd':
for item in files:
if not os.path.isdir(item):
for search in queries:
if search in item.lower():
try: os.remove(item)
except: perror(item)
else:
print '\nDeleting:\t{0}'.format(item)
print 'From . . .\n{0}\n'.format(newLocation)
# If not limit is put on the search, cycle through the items in the current
# directory again and enter the next folder. This occurs recursively.
if limit == False:
for item in files:
newLocation = location + os.sep + item
if os.path.isdir(newLocation):
try: os.chdir(newLocation)
except:
perror(newLocation)
continue
try: os.listdir(os.curdir)
except:
perror(newLocation)
continue
scan_dirs(newLocation, queries, action)
os.chdir('../')
else: return
###############################################################################
############################# MAIN FUNCTION ###################################
###############################################################################
if __name__ == '__main__':
queries = []
# Have the user enter the directory to begin the search.
while True:
print '\nFile paths must be entered in full.'
try: location = raw_input('Enter the directory path to begin search.\n--> ')
except: continue
if location == '.': continue
try: os.chdir(location)
except: continue
else: break
print '\nEnter keywords to search for. Enter nothing to stop query input.'
print 'You may choose to enter file extensions if you wish.'
# Have the user enter a list of keywords to search for.
# This is not heavily monitored, as it is up to the user to be aware of
# the potential drawbacks of certain search queries.
while True:
try: lookFor = raw_input(' - ').lower()
except: continue
if not len(lookFor) == 0: queries.append(lookFor)
else:
print '- Done taking keyword input -'
break
# Have the user decide whether they would like the search to occur
# in just files names, the file's content, or both.
while True:
print 'Please choose which action you would like to take.'
print 'Note that selecting "B" or "b" will not invoke the delete feature.'
try: decision = raw_input('\nFile Names(N) File Content(C) Names and Content(B) Delete(D): ')
except: continue
if decision in ['N', 'n', 'C', 'c', 'B', 'b', 'D', 'd']:
if decision in ['C', 'c', 'B', 'b']:
print '\nSearching file content is a very tedious and resource consuming task.'
print 'It could even cause the program to crash if run through a large directory.'
try: yn = raw_input('Are you sure you want to continue? (Y/n): ')
except: continue
if yn in ['N', 'n']: continue
elif yn not in ['Y', 'y']:
print 'Improper input.\nPlease start again.'
continue
if decision == 'D' or decision == 'd':
print 'The program will now proceed to delete files with the specified names.'
try: yn = raw_input('Do you really want to continue? (Y/n): ')
except: continue
else:
if yn == 'N' or yn == 'n': continue
elif yn not in ['Y', 'y']:
print 'Improper input.\nPlease start again.'
continue
while True:
limit = False
try: lim = raw_input('Would you like to limit the search to only the directory specified? (Y/n): ')
except: continue
else:
if lim in ['Y', 'y']: limit = True
elif lim in ['N', 'n']: limit = False
else: continue
break
print '\nBeginnning search.\n' + '-'*83
break
else: print 'Input not correct.\nTry again.\n\n'
scan_dirs(location, queries, decision, limit)
print '\n' + '-'*83 + '\nSearch Complete.\n\n'

Nice work. You may be interested in the python program grinhttp://pypi.python.org/pypi/grin, which is multiplatform, but produces a result similar to unix' grep command. A nice feature of grin is that it uses python regular expressions in the query. Also its source code may contain ideas to improve your program.

I did a bit of a touchup to the program I posted above. This new version does basically the same thing. I did however take out the ability to search file content and delete files (they could easily be added back on).
This version is a bit cleaner, a little shorter, easier to read, and hopefully a little easier to expand upon. It is also all called right from the command line, with no interaction during execution.

'''A program that can be used to quickly resolve whether or not a file exists
on the machine in a specifc area and or onwards.
Completed on: July 31, 2010.
Creator: _ryuurei (Travis Mullaly)
Contact: travis_m.cpp@live.com
'''
import os
import sys
from time import sleep
# Establish an error message to facilitate accurate use of the program.
USAGEMSG = 'To properly run this program, please execute it as such:\n\
python {0} [full directory path] [query1,query2,query3] [limit]\n\
An example is: python {0} /usr/bin python,src,lib,doc true\n\
\n\
query:\n\
A list of keywords to search for. Seperated only by one comma each.\n\
\n\
limit:\n\
Must be entered as the word "true" or the word "false". \n\
Denotes whether the search should be contained to the current \n\
directory (true) or not (false).'\
.format(sys.argv[0])
def perror(location):
'''A function to print a text-based error message.'''
print('-'*72)
print(' Access to {0} not allowed. '.format(location).center(72, '#'))
print('-'*72)
sleep(1.5)
def print_result(queryTerm, fileName, directory):
'''Print a message when a query term is found.'''
print('\nQuery found:\t{0}'.format(queryTerm))
print('In filename of\t{0}'.format(fileName))
print('In Directory . . .\n{0}\n'.format(directory))
def operation(fileList, queries, directory):
'''Perform a search through the items in the current directory.'''
for item in fileList:
if os.path.isfile(item):
for term in queries:
if term in item.lower() and not item.endswith('~'):
print_result(term, item, directory)
def scan(location, queries, limit=False):
'''Launches position-based recursion search algorithms.'''
# If the program cannot obtain a list of the current directorie's items,
# it cannot operate, and thus ceases functioning.
try: files = os.listdir(os.curdir)
except:
perror(location)
print('\nProgram cannot continue due to initialization error.\n')
sys.exit()
operation(files, queries, location)
# When appropriate, move to the next sub-directory.
if not limit:
for item in files:
newLocation = location + os.sep + item
if os.path.isdir(newLocation):
try: os.chdir(newLocation)
except:
perror(newLocation)
continue
try: os.listdir(os.curdir)
except:
perror(newLocation)
continue
scan(newLocation, queries)
else: return
def main():
'''The main function processes the program arguments and begins searching.'''
if len(sys.argv) != 4:
print('You did not enter the correct number of specifications.')
print(USAGEMSG)
sys.exit()
if sys.argv[3].lower() == 'true':
limit = True
elif sys.argv[3].lower() == 'false':
limit = False
else:
print('There was an error in your last specification.')
print('The value you assign limit must be true or false.')
print(USAGEMSG)
sys.exit()
queries = sys.argv[2].split(',')
for i in range(len(queries)):
queries[i] = queries[i].lower()
try: os.chdir(sys.argv[1])
except:
print('There was an error in the way your directory path was specified.')
print('Please remember to enter the path in full for most reliable performance.')
print(USAGEMSG)
sys.exit()
print('\nBeginning Search.' + '\n' + '-'*72)
scan(sys.argv[1], queries, limit)
print('-'*72 + '\nSearch Complete.\n')
if __name__ == '__main__':
main()

Hi. so this is actually a continuation from another question of mine[Here](https://www.daniweb.com/programming/software-development/threads/506795/dynamically-add-values-into-datagridview-cell-from-listbox-vb2010) but i was advised to start a new thread as the original question …

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...