0
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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Wickerman
  • 9
  • 4
  • `folderlist` is a python `list`. You cannot open a list. You can instead open a file name. – VooDooNOFX Nov 21 '13 at 05:26
  • You likely wanted `open(file).read()` instead of `open(folderlist).read()`. However, you shouldn't use the built-in name `file` for your variable name. It happens to share the name with a built-in for python. – VooDooNOFX Nov 21 '13 at 05:28
  • @VooDooNOFX: FWIW, it looks like the OP is using Python 3. If so, `file` isn't a builtin name any more and is a perfectly cromulent variable name. – DSM Nov 21 '13 at 05:37

1 Answers1

2

You are making multiple mistakes here:

First, try to indent your code correctly:

def is_file_contain_word(folderlist,query_word):
    for file in folderlist:
        if query_word in open(folderlist).read():
            print (file)
    print("finishing searching.")  

Then the real problem.
open(folderlist) should be open(file), as I believe that's your intention.
And that's one reason why the program won't run: open(folderlist) opens C:\Users\User\Documents\prog, which is a directory and will cause problem.(You probably thought this line opens all files contained in that folder)
BTW, don't use "file" as a var, it's a reserved word in python.

However, even though you fix this part, the original function still won't work.

def is_file_contain_word(folderlist,query_word):
    for f in folderlist:
        if query_word in open(f).read():
            print (f)
    print("finishing searching.") 

Why? If you check this folderlist(folderlist=os.listdir(path)), it would be something like:

["Prog's Movies", "Homework3.pdf", "blahblah.txt"]

Which is problematic because first, you can't open them because they aren't absolute path(Like C:\Users\God\Documents\prog\blahblah.txt), second, listdir would also list all directories(In this case "Prog's Movies" directory), which you can't use open to open them.

At last, if you are running this on command line, don't make two folderlist vars. I know in this case it won't cause problem, but it dose reduce your code's readibility and seem to confuse you a little bit.

How to fix it? How to list all files of a directory?
Here is a nice answer to how to open all files in a directory correctly.

Community
  • 1
  • 1
octref
  • 6,521
  • 7
  • 28
  • 44
  • 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) – Wickerman Nov 21 '13 at 05:39
  • @user3007632 Still not quite right. Plz read the answer that I linked to, as you only need to modify it a little bit to achieve the result you want. BTW, you need to learn how to format your code. Now it is unreadable. – octref Nov 21 '13 at 06:01
  • marked as the answer, sorry it took this long – Wickerman Oct 06 '21 at 04:40