python print random line of a text file code example
Example 1: Python program to read a random line from a file
import random
def randnum(fname):
with open(fname).read().splitlines() as
print(lines)
return random.choice(lines)
print(randnum('file1.txt'))
Example 2: Python program to read a random line from a file.
import random
def randnum(fname):
lines=open(fname).read().splitlines()
print(lines)
return random.choice(lines)
print(randnum('file1.txt'))