Readline() to read a single line at a time from a file

Posted under » Python » Django on 3 December 2025

There may be a time when you want to look for a word line by line in a text file. Here is one way

with open(r'email.txt', 'r') as fp:
    lines = fp.readlines()
    for row in lines:
        word = 'gaybo011@'  # String to search for
        if row.find(word) != -1:
            print('String exists in file')
            print('Line Number:', lines.index(row))

In Django, instead of setting paths, it is better to import JSON or text file using `import requests' like so.

import requests

url = "http://empat.com/kaput.txt"
response = requests.get(url)

# The content of the text file is in response.text
file_content = response.text

# Print the content
print(file_content)

# You can also iterate over the lines directly
for line in response.text.splitlines():
    print(line)

You can create the text file using Django.

web security linux ubuntu Raspberry   git   javascript css python django drupal php apache mysql  MongoDB AWS data  ML AI