How can one use the readlines()
method to read a file in a random shuffled manner i.e. random.shuffle()
file = open(filename)
data = file.readlines()
file_length = len(data)
Get them into a list with lines = file.readlines()
, and then random.shuffle(lines)
that list (import random
module).
You can store the entire file as a list of lines with:
f = open(filename)
data = f.read() # the whole file in one string
lines = data.split('\n')
Then use random to access lines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With