Skip to content

Instantly share code, notes, and snippets.

@markjenkins
Created October 26, 2021 19:37
Show Gist options
  • Save markjenkins/abab90cc2f7cbf7c7c7548e0e75cdccb to your computer and use it in GitHub Desktop.
Save markjenkins/abab90cc2f7cbf7c7c7548e0e75cdccb to your computer and use it in GitHub Desktop.
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