Word Detect Form A File
Welcome to CodingFizz,
In this tutorial, We will go to make a Word Detector using Python language.
This is a python practice tutorial for beginners.
How Its Work:
If we have many files and we need to search a word for any purpose in the files.
There are many ways to search. But if you are a programmer then you can make it easy with the help of using python.
It's too easy to program firstly you need to import os module and make a function and read the files.
There are 2 files and I need to search "Ashish" in the file.
In this "Ashish" word is in Lower case.
You can see the code.
Source Code:
import os
def word_detect(filename):
with open(filename,"r") as f:
content=f.read()
if "ashish" in content.lower():
return True
else:
return False
if __name__=="__main__":
dir_content = os.listdir()
nfile=0
for file in dir_content:
if file.endswith("txt"):
print(f'\nDetecting Word in {file}')
Word= word_detect(file)
if (Word):
print(f"\nWord found in {file}")
nfile=nfile+1
else:
print(f"\nWord not found in {file}")
print(f'\n{nfile} Number of files found Word!!!!\n')
Detecting Word in hello.txt
Word found in hello.txt
Detecting Word in me.txt
Word found in me.txt
Detecting Word in not.txt
Word not found in not.txt
2 Number of files found Word!!!!
0 Comments