Reputation: 49
Guys I was trying to search for a word that will be entered by the user in a list of files in a folder and so far I have this code :
import os
folderpath = "C:\\Users\\user\\Desktop\\Documents"
word = input("Choose a word : ")
for(path, dirs, files) in os.walk(folderpath, topdown=True):
for filename in files:
filepath = os.path.join(path, filename)
with open(filepath, 'r') as f:
info = f.readlines()
for line in info:
if word in line:
print( filename + ":" + "[1]" )
else:
print(filename + "[0]")
the output is the name of each file 10 times then 1,0,1,0... respectively(ex: Doc1[1] , Doc1[0] , Doc1[1]....). It looks like nothing is breaking the loop. Please any help
Upvotes: 0
Views: 48
Reputation: 1724
Your code is printing output for every line in every file, not just 10 times. I suspect your files are all 10 lines long if that is the case.
The following code just tests str(info)
for the word, printing one match for each file:
import os
folderpath = "C:\\Users\\user\\Desktop\\Documents"
word = input("Choose a word : ")
for(path, dirs, files) in os.walk(folderpath, topdown=True):
for filename in files:
matched = 0
filepath = os.path.join(path, filename)
with open(filepath, 'r') as f:
info = f.readlines()
if word in str(info):
matched = 1
print("{}: [{}]".format(filename, matched))
If you wish your test to be case-insensitive, simply replace:
if word in str(info):
With:
if word.casefold() in str(info):
If you'd like to have an actual count of occurrences for each file in your output, you can do something like:
import os
folderpath = "C:\\Users\\user\\Desktop\\Documents"
word = input("Choose a word : ")
for(path, dirs, files) in os.walk(folderpath, topdown=True):
for filename in files:
count = 0
filepath = os.path.join(path, filename)
with open(filepath, 'r') as f:
info = f.readlines()
if word in str(info):
for line in info:
if word in line:
count += 1
print("{}: [{}]".format(filename, str(count)))
Take a look at the modified line 13 below, and you'll see each match along with the filename.
import os
folderpath = "C:\\Users\\user\\Desktop\\Documents"
word = input("Choose a word : ")
for(path, dirs, files) in os.walk(folderpath, topdown=True):
for filename in files:
filepath = os.path.join(path, filename)
with open(filepath, 'r') as f:
info = f.readlines()
for line in info:
if word in line:
print( filename + ":" + "[1]:", line )
else:
print(filename + "[0]")
Upvotes: 1