The find() method is called to check if the search string is present in a line. The files that match the file type are opened and each line is read in loop. If no file type is input then program will search all files in the directory. ini the program will check if the filename ends with the extension. conf etc., If the user inputs a file type, for example.
The file type can be any text file format such as. In the next step, each file in the directory is checked and if it matches the file type. If the path is invalid or if it does not exist then the search path is set to the current directory. If the directory path does not containing a directory separator, which is forward slash (/) in case of Linux and either a backward slash() or forward slash (/) in case if Windows, then it is appended to search path. The user is prompted to enter a directory path, file type and a text string to be searched. The program uses the os module to get the files from a directory.
The program outputs the filenames that contain the search string and also the line number and column number where the text appears in the file. Additionally, by entering the file type, the search can be restricted to a subset of the files in the directory. The user inputs the directory path and a text string to search. This is a Python program to search for a specified text in all files of a directory.