Search code examples
pythonfilerandomtext-filesreadline

How do I print a random full link from a text file?


In the text file "unt.txt" there's 38 links all on their own lines. How do I print a random link from the file? This code below just returns a random amount of characters in a link and not the entire link.

from os import close, read
import random

rnd_num = random.choice(range(1,39))

File = open("unt.txt", "r")
print (File.readline(rnd_num))
File.close()

Solution

  • This worked:

    lines = open('text.txt').read().splitlines()
    line = random.choice(lines)
    print(line)