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

Time:01-20

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()

CodePudding user response:

This should work.

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

import random

print(random.choice(urls))

CodePudding user response:

This worked:

lines = open('text.txt').read().splitlines()
line = random.choice(lines)
print(line)
  •  Tags:  
  • Related