Smaple of recursive directory walker

T

Traveler

Hello,

At work I have a directory of about 50 large text files and i need to
search thru them for 10 separate words and print how many were found
in total.

I am new to python so could somebody please show me some sample code
that would help me get this done and i will work from that.

Thanks.
 
A

Ant

At work I have a directory of about 50 large text files and i need to
search thru them for 10 separate words and print how many were found
in total.

I am new to python so could somebody please show me some sample code
that would help me get this done and i will work from that.

Assuming it's primarily the directory walk you need help with,
something like the following should help:

for root, dirs, files in os.walk('~/mydir'):
for file in [f for f in files if f.endswith(".txt")]:
fh = open(file)
for line in fh:
# Search for words.
fh.close()
 
T

Traveler

yes this is great i will work from that but how can i use say a list
to pass 10 words?

mylist = ['word1','word2','word3','word4']



At work I have a directory of about 50 large text files and i need to
search thru them for 10 separate words and print how many were found
in total.

I am new to python so could somebody please show me some sample code
that would help me get this done and i will work from that.

Assuming it's primarily the directory walk you need help with,
something like the following should help:

for root, dirs, files in os.walk('~/mydir'):
for file in [f for f in files if f.endswith(".txt")]:
fh = open(file)
for line in fh:
# Search for words.
fh.close()
 
A

Ant

Traveler said:
yes this is great i will work from that but how can i use say a list
to pass 10 words?

mylist = ['word1','word2','word3','word4'] ....
for root, dirs, files in os.walk('~/mydir'):
for file in [f for f in files if f.endswith(".txt")]:
fh = open(file)
for line in fh:
# Search for words.
fh.close()

The following will allow you to search a line of text for one of a list
of words.

import re

line = "line of text"
mylist = ["bogus", "text", "here"]

p = re.compile(r"\b(%s)\b" % '|'.join(mylist))
m = p.search(line)
if m: print "Found %s" % m.group(1)

Alternatively, the following will give a list of all words in a string
that appear in the list:

words_found = [word for word in re.split(r"\W+", line) if word in
mylist]
 
T

Traveler

Thank you very much that is what i was looking for.


yes this is great i will work from that but how can i use say a list
to pass 10 words?

mylist = ['word1','word2','word3','word4'] ...
for root, dirs, files in os.walk('~/mydir'):
for file in [f for f in files if f.endswith(".txt")]:
fh = open(file)
for line in fh:
# Search for words.
fh.close()

The following will allow you to search a line of text for one of a list
of words.

import re

line = "line of text"
mylist = ["bogus", "text", "here"]

p = re.compile(r"\b(%s)\b" % '|'.join(mylist))
m = p.search(line)
if m: print "Found %s" % m.group(1)

Alternatively, the following will give a list of all words in a string
that appear in the list:

words_found = [word for word in re.split(r"\W+", line) if word in
mylist]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top