0 votes
in Python Flask by
How will you read a random line in a file?

1 Answer

0 votes
by

We can read a random line in a file using the random module.

For example:

import random

def read_random(fname):

lines = open(fname).read().splitlines()

return random.choice(lines)

print(read_random (‘hello.txt’))

...