Skip to content

Instantly share code, notes, and snippets.

@igilham
Created January 5, 2011 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igilham/766511 to your computer and use it in GitHub Desktop.
Save igilham/766511 to your computer and use it in GitHub Desktop.
Read a random line from a file
#!/usr/bin/env python
import os
import random
import sys
def randline(fname):
result = ''
if os.path.exists(fname):
result = random.choice([line for line in open(fname)])
return result
def main(*args):
for arg in args[1:]:
print(randline(arg))
return 0
if __name__ == "__main__":
sys.exit(main((*sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment