Skip to content

Instantly share code, notes, and snippets.

@kathawala
Created December 15, 2014 05:47
Show Gist options
  • Save kathawala/3373632f657f51b9604f to your computer and use it in GitHub Desktop.
Save kathawala/3373632f657f51b9604f to your computer and use it in GitHub Desktop.
Prints and erases 5 random lines from a file
# Reads out 5 random lines from a file and erases them
import os.path
import random
dictionary = open("/home/farhan/bin/words", "r")
words = set()
last_pos = dictionary.tell()
rand_words = list(dictionary)
for i in range(0, 5):
word = random.choice(rand_words)
words.add(word)
print(word, end='')
dictionary.seek(last_pos)
new_dict = open("/home/farhan/bin/words.bak", "w")
for line in dictionary:
if line not in words:
new_dict.write(line)
os.remove("/home/farhan/bin/words")
os.rename("/home/farhan/bin/words.bak", "/home/farhan/bin/words")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment