Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And where is your code? If you don't show you are trying nobody will work for free.

  • #3
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    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."
    Last edited by edward_pedro; 09-15-2009 at 02:37 PM.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    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()

  • #5
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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
    Last edited by edward_pedro; 09-16-2009 at 07:53 AM. Reason: and another thing...(not the novel)

  • #6
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    This will make both the search string and the current line lowercase during the comparison:
    Code:
    if b.lower() in line.lower():
    It might not be the best way to go but it's worth a shot.
    I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
    ** Independent Multimedia Artist in Pasig **


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •