A chronological folder backup script for those who want flexibility.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

import os
import shutil
class Excluder:
'''
@about:
Handles the deletion of files or directories from a specified source path.
@warning:
First off, read the f--king README. Second, this will permanently delete the
specified files and directories from any subdirectory of the folder you provide
when you instantiate Chronos. Consider that you will delete ALL occurances of ANY
file OR directory you give this class. You have been sufficiently warned.
@params:
path: string
The source path.
excluded_files: list
A list of filenames to delete if found in the walk.
excluded_dirs: list
A list of directory names to delete if found in the walk.
'''
def __init__(self, excluded_files, excluded_dirs):
self.excluded_files = excluded_files
self.excluded_dirs = excluded_dirs
self._deleted = []
self.path = None
def _parse_files(self, root, files):
for file in files:
path = os.path.join(root, file)
if file in self.excluded_files:
os.remove(path)
self._deleted.append(path)
def _parse_dirs(self, root, dirs):
for dir in dirs:
path = os.path.join(root, dir)
if dir in self.excluded_dirs:
shutil.rmtree(path)
self._deleted.append(path)
def _walk(self):
for root, dirs, files in os.walk(self.path):
self._parse_files(root, files)
self._parse_dirs(root, dirs)
def _report(self):
for item in self._deleted:
print(f'Deleted: {item}')
self._deleted.remove(item)
def __call__(self, path):
self.path = path
self._walk()
self._report()