Skip to content

Instantly share code, notes, and snippets.

@SpirosArk
Created March 29, 2021 06:00
Show Gist options
  • Save SpirosArk/b3587393836781066206733d9cc9d6a3 to your computer and use it in GitHub Desktop.
Save SpirosArk/b3587393836781066206733d9cc9d6a3 to your computer and use it in GitHub Desktop.
A python code that searches given files for given strings. Doesn't print the number of the line that 'String_To_Search' was found. Prints the whole line of the string given (if found).
def check_if_string_in_file(file_name, string_to_search):
""" Check if any line in the file contains given string """
# Open the file in read only mode
a = False
with open(file_name, 'r') as read_obj:
# Read all lines in the file one by one
for line in read_obj:
# For each line, check if line contains the string
if string_to_search in line:
print ('Entry in line')
print(line)
a = True
if a == False:
print("No entry of your line in file")
check_if_string_in_file('File_To_Search', 'String_To_Search')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment