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

Related questions

0 votes
asked Oct 1, 2023 in Python Flask by sharadyadav1986
0 votes
asked Oct 2, 2023 in Python Imaging Library by sharadyadav1986
...