Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import time
- class File:
- def __init__(self, name, base_path):
- self.name = name
- self.path = os.path.join(base_path, self.name)
- self.mtime = os.path.getmtime(self.name)
- def __eq__(self, other):
- if self.name == other.name:
- return True
- return False
- def __repr__(self):
- return self.name
- class Directory:
- def __init__(self, path):
- self.path = path
- self.files = self._get_files()
- self.time = time.time()
- def __contains__(self, other_file):
- for file in self.files:
- if file == other_file:
- return True
- return False
- def _get_files(self):
- return [File(f, self.path) for f in os.listdir(self.path) if os.path.isfile(f)]
- def get_by_name(self, name):
- ind = self.files.index(name)
- return self.files[ind]
- def check_files(self):
- tmp = 'Файл {} {}, путь к файлу: {}'
- updated_file_list = self._get_files()
- for file in self.files:
- if file not in updated_file_list:
- print(tmp.format(file, 'удален', file.path))
- for file in updated_file_list:
- if file in self.files:
- prev = self.get_by_name(file)
- if file.mtime > prev.mtime:
- print(tmp.format(file, 'изменен', file.path))
- else:
- print(tmp.format(file, 'создан', file.path))
- self.files = updated_file_list
- if __name__ == '__main__':
- while True:
- target_dir = input('Введите путь к нужно директории(enter - текущая папка)') or os.getcwd()
- if not os.path.isdir(target_dir):
- print('Такой директории не существует')
- break
- d = Directory(target_dir)
- while True:
- d.check_files()
- time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment