

# If path does not exist, set search path to current directoryįor fname in os.listdir(path=search_path): If not (search_path.endswith("/") or search_path.endswith("\\") ): # Append a directory separator if not already present Search_str = input("Enter the search string : ") Search_path = input("Enter directory path to search : ") The filename, line number, index and the whole line that contains the string is output to the user console. If found, the find method returns index where the string was found. 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. This article introduces see.py, which helps in accomplishing this task. The only alternative, then, is to make a command that will search the string.
#Linux search files for text string not windows#
While Linux has the grep command, Windows does not have an equivalent.

The program uses the os module to get the files from a directory. Searching text strings from files in a given folder is easily accomplished by using Python in Windows.

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.
