Get them into a list with lines = file.readlines() , and then random.shuffle(lines) that list (import random module).
We can shuffle lines in the file in linux using following commands. shuf; sed and sort; awk; python. As an example we will
in enumerate(..., 2) iterator produces the sequence 2, 3, 4.
That would do two passes in the file, but still avoid storing the whole file in memory. Another advantage over GNU shuf is that it preserves the order of the lines in ...
You can use shuf . On some systems at least (doesn't appear to be in POSIX). As jleedev pointed out: sort -R might also be an option. On some ...
Write a Python program to read a random line from a file. Sample Solution:- Python Code: import random def random_line(fname): lines ...
In addition to the shuf and sort -R, we can also shuffle lines in a file
traverse file and find line number $rnd i=0 while read -r line; do ((i++)) [ $i -eq $rnd ]
Introduction to Programming in Python by Robert Sedgewick, Kevin Wayne,
[ FILE]... Randomize the lines of a file (or stdin). -c, --count=N select N lines