[Solved] how do u make a program that checks many txt files [closed]


I’m not sure if this is what you are asking, but I interpreted this as list the files in a given directory, and then check if the input is in the file.

import os
string = input("Please type the input ") 
directory = "c://files//python" 
for file in os.listdir(directory): 
    if file.endswith(".txt"): 
        filecontent = open(file, "r")
        if string in filecontent.read():
            print("The file that matches the input was found at" + file)

Line 2: Set the string to whatever the input is

Line 3: Use double slashes to avoid escape characters http://learnpythonthehardway.org/book/ex10.html

Line 4: Goes through every file in the directory. listdir returns a list with all the file names

Line 5: Can change this to .jpg, .png, etc

Line 6: Opens the file contents

Line 7: Reads the file’s contents into a string, then check if the input is in said string

solved how do u make a program that checks many txt files [closed]