Matt23

Removing unnecessary lines from file

May 19th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import os.path
  3. import time
  4. import re
  5.  
  6. getTime = lambda: int(round(time.time() * 1000))
  7.  
  8. output_file = 'cleared_hosts' #file where content will be placed
  9. start = getTime()
  10.  
  11. def getRegex():
  12.     regex = ''
  13.     with open('urls_list.txt', 'r') as body: #file with list of lines to remove
  14.         for line in body:
  15.             regex += line.strip('\n') + '|'
  16.     return re.compile(regex[:-1])
  17.  
  18. def prepareHosts(regex):
  19.     with open('hosts', 'r') as content: #file we want to remove lines from
  20.         for line in content:
  21.             if not regex.search(line):
  22.                 writeToFile(line)
  23.  
  24. def writeToFile(text):
  25.     with open(output_file, 'a') as file_:
  26.         file_.write(text)
  27.  
  28.  
  29. if os.path.isfile(output_file):
  30.     os.remove(output_file)
  31.  
  32. prepareHosts(getRegex())
  33. print("Took {0}ms".format(getTime() - start))
Advertisement
Add Comment
Please, Sign In to add comment