Guest User

Untitled

a guest
Apr 21st, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. import os
  2. import time
  3.  
  4.  
  5. class File:
  6.     def __init__(self, name, base_path):
  7.         self.name = name
  8.         self.path = os.path.join(base_path, self.name)
  9.         self.mtime = os.path.getmtime(self.name)
  10.  
  11.     def __eq__(self, other):
  12.         if self.name == other.name:
  13.             return True
  14.         return False
  15.  
  16.     def __repr__(self):
  17.         return self.name
  18.  
  19.  
  20. class Directory:
  21.     def __init__(self, path):
  22.         self.path = path
  23.         self.files = self._get_files()
  24.         self.time = time.time()
  25.  
  26.     def __contains__(self, other_file):
  27.         for file in self.files:
  28.             if file == other_file:
  29.                 return True
  30.             return False
  31.  
  32.     def _get_files(self):
  33.         return [File(f, self.path) for f in os.listdir(self.path) if os.path.isfile(f)]
  34.  
  35.     def get_by_name(self, name):
  36.         ind = self.files.index(name)
  37.         return self.files[ind]
  38.  
  39.     def check_files(self):
  40.         tmp = 'Файл {} {}, путь к файлу: {}'
  41.  
  42.         updated_file_list = self._get_files()
  43.  
  44.         for file in self.files:
  45.             if file not in updated_file_list:
  46.                 print(tmp.format(file, 'удален', file.path))
  47.  
  48.         for file in updated_file_list:
  49.  
  50.             if file in self.files:
  51.                 prev = self.get_by_name(file)
  52.                 if file.mtime > prev.mtime:
  53.                     print(tmp.format(file, 'изменен', file.path))
  54.             else:
  55.                 print(tmp.format(file, 'создан', file.path))
  56.  
  57.         self.files = updated_file_list
  58.  
  59.  
  60. if __name__ == '__main__':
  61.  
  62.     while True:
  63.         target_dir = input('Введите путь к нужно директории(enter - текущая папка)') or os.getcwd()
  64.  
  65.         if not os.path.isdir(target_dir):
  66.             print('Такой директории не существует')
  67.         break
  68.  
  69.     d = Directory(target_dir)
  70.  
  71.     while True:
  72.         d.check_files()
  73.         time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment