Hello all! Sorry if this is probably a simple fix but I can't think of one. I am trying to take a command line argument (in this case a name) and search through a file hierarchy and keep track of the number of times that name comes up. I was just wondering how to store the CL input and then search through files using a regular expression with that input as my search key.
A:
import sys
import os
the_name= sys.argv[1]
count=0
for r,d,f in os.walk("/mypath"):
for file in f:
if the_name in file:
count+=1
If you want to search IN the file themselves,
for r,d,f in os.walk("/mypath"):
for file in f:
for line in open(os.path.join(r,file)) :
if the_name in line:
count+=line.count(the_name)
ghostdog74
2010-10-15 02:34:32
Will this search through each file, or just the file names?
amazinghorse24
2010-10-15 02:39:31
Just the file names.
Swiss
2010-10-15 02:44:59
what does the "d" do in `r,d,f`?
amazinghorse24
2010-10-15 02:59:54
just a place holder for directories returned by `os.walk()`, please read the `os` module doc to find out.
ghostdog74
2010-10-15 03:02:33