Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get N random lines from a file
#!/usr/bin/env python3
# call with two arguments, the path to the file to read lines from and
# the number of lines
from sys import argv
from random import SystemRandom
with open(argv[1]) as f:
lines = f.readlines()
r = SystemRandom()
print( ''.join( r.sample(lines, int(argv[2])) ), end='' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment