How to turn a txt file into a list with Python

Posted on: March 4th, 2023
By: Tadeo Martinez

Use readlines() to use the break lines of the file and turn it into an item in a list.

with open('file.txt') as file:
    content = file.readlines()
    random_line = random.choice(content)

Have any questions or comments? Write them below!


Leave a Reply

Your email address will not be published. Required fields are marked *