Matt23

Removing unnecessary lines from file v2

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