Suppose I have a file containing some lines:
line 1 ...
line 2 ...
...
line n ...
Is it possible to have another file where the order of the lines will be randomly mixed ?
The random module is your friend:
import random
with open("infile.txt") as f:
lines = f.readlines()
random.shuffle(lines)
with open("outfile.txt", "w") as f:
f.writelines(lines)
should do.
1) read the file 2) store each line in a string array 3) shuffle string array 4) write file
I think that is what you're asking for?
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