Created
December 15, 2014 05:47
-
-
Save kathawala/3373632f657f51b9604f to your computer and use it in GitHub Desktop.
Prints and erases 5 random lines from a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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