Skip to content

Instantly share code, notes, and snippets.

@markjenkins
Created October 26, 2021 19:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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