import os
path=r'C:\Users\User\Documents\prog'
folderlist=os.listdir(path)
def is_file_contain_word(folderlist,query_word):
for file in folderlist:
if query_word in open(folderlist).read():
print (file)
print("finishing searching.")
query_word=input("please enter the keyword:")
is_file_contain_word(folderlist,query_word)
This is what I have so far. It has returned that I have type error:
invalid file: ['1.doc', '1.odt', 'new.txt']
I got this error when I swapped path with folderlist in the open
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\God\\Documents\\prog'
import os
This is my new code:
import os
path=r'C:\Users\God\Documents\prog'
folderlist=os.listdir(path)
print(folderlist)
def is_file_contain_word(folderlist,query_word):
for i in os.listdir(path):
if query_word in i:
print("Found %s" %query_word)
else:
print("not found")
query_word=input("please enter the keyword:")
is_file_contain_word(path,query_word)
This goes through each of the 3 files to search for the filename. It only stops when it finds it or it goes through it.