January 10, 2013
Random Python Code of the Day
Written by
David Kennedy
Penetration Testing
Security Testing & Analysis
Needed a quick way to open up a text file and randomize each line and spit out a new file. Here's a quick python snippet to do just that:
import random
with open('file.txt', 'r') as infile: lines = infile.readlines()
random.shuffle(lines)
with open('file_random.txt', 'w') as outfile: outfile.writelines(lines)
Vala! The output will randomize each line for you.