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. 4 3281
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()
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']
On 29 Jul 2006 12:01:03 -0700, "Ant" <an****@gmail.comwrote:
>
>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()
Traveler wrote:
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]
Thank you very much that is what i was looking for.
On 30 Jul 2006 13:18:51 -0700, "Ant" <an****@gmail.comwrote:
> Traveler wrote:
>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]
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Max M |
last post by:
I guess that the best approach is calling a shell tool with something
like os.popen(). But I cannot seem to find any free tools.
Winzip has a command line option, but for registered users only....
|
by: Michael Foord |
last post by:
I've been using the excellent XMLObject and have unfortunately come up
against what *looks* to me like a bug - although it's very possible
that the problem is mine !!
I want to use XML object...
|
by: alanwo |
last post by:
For recursive search for files, like
http://support.microsoft.com/default.aspx?scid=KB;EN-US;306666, it may
lead to "out of stack" error if searching too many files, say millions
of files.
Do you...
|
by: betterdie |
last post by:
Dear guru
I want to delete all file and folder recursivly under php code, can
anyone give me commend for this.
Thank very much
|
by: Elmo Watson |
last post by:
Is there a way, with the System.IO class, to do a recursive list of a
directory structure?
For instance, in DirectoryInfo, you have GetDirectories and GetFiles ....
In Directory, you have...
|
by: Paul |
last post by:
I'm trying to make get my app to delete all the files in a specified folder
and all the files within the folders of the specified folder.
e.g.
Folder 1 contains three files (File1, File2, File3)...
|
by: Jandre |
last post by:
Hi
I am a python novice and I am trying to write a python script (most of
the code is borrowed) to Zip a directory containing some other
directories and files. The script zips all the files fine...
|
by: Martin Marcher |
last post by:
Hello,
I'm playing around with os.walk and I made up del_tree(path) which I
think is correct (in terms of the algorithm, but not as python wants
it :)).
As soon as some directory is deleted...
|
by: Gordon |
last post by:
I'm trying to remove a directory and all its contents from within a
script. I wrote a recursive function to take care of it, but when I
run it I get random "Directory not empty" error messages.
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |