Search code examples
python

How to read the first letter from the last line in a txt file in python


How can I read the first letter from the last line in a txt file in Python?


Solution

  • This is a solution, although maybe not the best since it is not memory efficient since it reads all the contents of a text file. Reverse reading is probably too complex for what you are asking for.

    with open('text.txt', 'r') as f:
        print(list(f)[-1][0])