Helpful Information
 
 
Category: Python
search file for string

Hey all. I'm new to this forum and Python.

I need a program that can search an inputted keyword through all the files that end with the ".txt" extension inside a given folder. The output should include the name of the files which contain the keyword, the sentences which contain the keyword (match exactly the word and not case-sensitive.
Lastly, the the output should be saved in a file named "Search Result.txt".

For example:

I input:

like

The output should look similar to:

FirstFile.txt:
...the word like has a very flexible range of uses...
...With LIKE you can use the following two wildcard characters in the pattern...
...Official site for People Like Us and Vicki Bennett...
...Celebs and Famous People who looks like other, um, things and stuff...

AnotherFile.txt:
...like Folke Rabe and his kid brother...
...Does your cat look like Adolf Hitler...

THANKS. :D

And where is your code? If you don't show you are trying nobody will work for free.

DAMN. There's another rule. If the input is "like" and the line is:


I'm pretty sure. Yes, that this is like the time when I went out.

instead of printing the entire line with "..." on the sides, this must be printed:


... that this is like the time when I went out ...

No punctuation marks apparently. :(

This is what I have so far. 2 problems:

1.) I don't really know how to search folders and files in folders.

2.) When I run the program, Python displays:


Traceback (most recent call last):
File "<pyshell#88>", line 1, in <module>
main()
File "<pyshell#87>", line 16, in main
for file in open(a):
TypeError: coercing to Unicode: need string or buffer, list found


def main():
d = ""
print "This program allows you to input a keyword..."
print
print "...to be searched through files in the folder Python26 of drive C."
x = "1"
while x == "1":
print
a = os.listdir("C:\Python26")
print
b = raw_input("What would you like to search for?: ")
print
print "Keyword: ", b,
print
print
for file in open(a):
for line in file:
if b in line:
print file, ":"
print
print "\t", "...", line, "...",
print
d = d + a + ":" + "\n" + "\t" + "..." + line + "..." + "\n"
c = open(r"C:\Python26\SearchResult.txt", "w")
c.write(d)
c.close()
print
x = raw_input("Would you like to use this program again? (Typing anything other than 1 ends the program.): ")
print
print "Thank you for using this program."

from os import listdir
from os.path import join, isfile

def main():
print "This program allows you to input a keyword..."
print
print "...to be searched through files in the folder Python26 of drive C."
path = r"/path/to/read/directory"
entries = [join(path, entry) for entry in listdir(path)]
files = filter(isfile, entries)
c = open(join("/path/to/write/directory", "SearchResult.txt"), "w")
print
print
b = raw_input("What would you like to search for?: ")
print
print "Keyword: ", b,
print
print
for file in files:
f = open(file, 'r')
for line in f.readlines():
if b in line:
print file, ":"
print
print "\t", "...", line, "...",
print
c.write(file + ":" + "\n" + "\t" + "..." + line + "..." + "\n")
f.close()
print
c.close()
print
print "Thank you for using this program."

main()

THANKS SO MUCH EVERYONE. Just one last detail. When printing the line with the given keyword it's supposed to be between punctuation marks :(

For example, if I input "TAIL", instead of printing:


... cat's tail is white. Then I said ...

I oughta print:


... s tail is white ...

please help :)

This is what I have so far:




from os import listdir
from os.path import join, isfile

def main():
print "This program allows you to input a keyword..."
print
print "...to be searched through files in the folder Python26 of drive C."
x = "1"
while x == "1":
path = r"C:\Python26"
entries = [join(path, entry) for entry in listdir(path)]
files = filter(isfile, entries)
c = open(join("C:\Python26", "SearchResult.txt"), "w")
print
print
b = raw_input("What would you like to search for?: ")
print
print "Keyword: ", b,
print
print
for file in files:
f = open(file, 'r')
for line in f.readlines():
if b in line:
print file, ":"
print
print "\t", "...", line, "...",
print
c.write(file + ":" + "\n" + "\t" + "..." + line + "..." + "\n")
f.close()
print
c.close()
print
x = raw_input("Type 1 then press enter to run the program again.")
print "Thank you for using this program."

main()



and another thing...(not the novel)

the search must be case INsensitive O.O

This will make both the search string and the current line lowercase during the comparison:

if b.lower() in line.lower():
It might not be the best way to go but it's worth a shot. :)










privacy (GDPR)