0

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()
spiffy
  • 29
  • 6
  • Does this answer your question? [How do I read a random line from one file?](https://stackoverflow.com/questions/3540288/how-do-i-read-a-random-line-from-one-file) – kpie Jan 19 '22 at 22:36

2 Answers2

2

This should work.

with open("unt.txt","r") as f:
    urls = f.readlines()

import random

print(random.choice(urls))
kpie
  • 9,588
  • 5
  • 28
  • 50
0

This worked:

lines = open('text.txt').read().splitlines()
line = random.choice(lines)
print(line)
spiffy
  • 29
  • 6