<jupyter_start><jupyter_text>Auto_Backup  Imports<jupyter_code>import argparse
import gzip
import os
import shutil
import sys
import threading
def parse_input():
    """ Input Parsing for Command Line Interface """
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--target', nargs=1, required=True,
                        help='Target Backup folder')
    parser.add_argument('-s', '--source', nargs='+', required=True,
                        help='Source Files to be added')
    parser.add_argument('-c', '--compress', nargs=1, type=int,
                        help='Gzip threshold in bytes, Default 1024KB', default=[1024000])
    # Default Threshold is 1024KB
    

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit()

    return parser.parse_args()

def size_if_newer(source, target):
    """ If newer it returns size, otherwise it returns False """

    src_stat = os.stat(source)
    try:
        target_ts = os.stat(target).st_mtime
    except FileNotFoundError:
        try:
            target_ts = os.stat(target + '.gz').st_mtime
        except FileNotFoundError:
            target_ts = 0

    # The time difference of one second
    return src_stat.st_size if (src_stat.st_mtime - target_ts > 1) else False
def threaded_sync_file(source, target, compress):
    """ Threading for Synchronized Files """
    size = size_if_newer(source, target)

    if size:
        thread = threading.Thread(target=transfer_file,
                                  args=(source, target, size > compress))
        thread.start()
        return thread
def sync_file(source, target, compress):
    size = size_if_newer(source, target)

    if size:
        transfer_file(source, target, size > compress)
def transfer_file(source, target, compress):
    """ Either copy or compress and copies the file """

    try:
        if compress:
            # Writing in Binary mode
            with gzip.open(target + '.gz', 'wb') as target_fid:
                with open(source, 'rb') as source_fid:
                    target_fid.writelines(source_fid)
            print('Compress {}'.format(source))
        else:
            shutil.copy2(source, target)
            print('Copy {}'.format(source))
    except FileNotFoundError:
        os.makedirs(os.path.dirname(target))
        transfer_file(source, target, compress)
def sync_root(root, arg):
    target = arg.target[0]
    compress = arg.compress[0]
    threads = []

    for path, _, files in os.walk(root):
        for source in files:
            source = path + '/' + source
            threads.append(threaded_sync_file(source,
                                              target + source, compress))
    #            sync_file(source, target + source, compress)
    for thread in threads:
        thread.join()
if __name__ == '__main__':
    arg = parse_input()
    print('------------------------- Start copy -------------------------')
    print('______________________________________________________________')
    for root in arg.source:
        sync_root(root, arg)
    print('______________________________________________________________')
    print('------------------------- Done Done! -------------------------')<jupyter_output>usage: ipykernel_launcher.py [-h] -t TARGET -s SOURCE [SOURCE ...]
                             [-c COMPRESS]
ipykernel_launcher.py: error: the following arguments are required: -t/--target, -s/--source<jupyter_text>ImportantKindly Ignore the above errors, the traceback is cause am not sure how to run a Jupyter Notebook Instance from CLI.For Reference I have attached a the output from a native python terminal.<jupyter_code>!python Auto_backup.py -t ./MIREX_Backup -s ./MIREX_Dataset -c 100000<jupyter_output>------------------------- Start copy -------------------------
______________________________________________________________
Copy ./MIREX_Dataset/categories.txt
Copy ./MIREX_Dataset/split-by-categories-lyrics.bat
Copy ./MIREX_Dataset/split-by-categories-midi.bat
Copy ./MIREX_Dataset/clusters.txt
Copy ./MIREX_Dataset\Audio/Place  file in folder.bat
Compress ./MIREX_Dataset/dataset info.csv
Compress ./MIREX_Dataset\Audio\Cluster 1\Agressive/743.mp3
Compress ./MIREX_Dataset\Audio\Cluster 1\Agressive/742.mp3
Compress ./MIREX_Dataset\Audio\Cluster 1\Boisterous/001.mp3
Compress ./MIREX_Dataset\Audio\Cluster 1\Agressive/741.mp3
Compress ./MIREX_Dataset\Audio\Cluster 1\Agressive/750.mp3
Compress ./MIREX_Dataset\Audio\Cluster 1\Agressive/744.mp3
Compress ./MIREX_Dataset\Audio\Cluster 1\Boisterous/008.mp3
Compress ./MIREX_Dataset\Audio\Cluster 1\Agressive/748.mp3
Compress ./MIREX_Dataset\Audio\Cluster 1\Boisterous/003.mp3
Compress ./MIREX_Dataset\Audio\Cluster 1\Agressive/757.mp3
Compress ./MIR[...]<jupyter_text>Size ComparisionBefore Compressing<jupyter_code>total_size = 0
start_path = './MIREX_Dataset'  # To get size of current directory
for path, dirs, files in os.walk(start_path):
    for f in files:
        fp = os.path.join(path, f)
        total_size += os.path.getsize(fp)
print("Directory size: " + str(total_size))<jupyter_output>Directory size: 334731974<jupyter_text>After Compressing<jupyter_code>total_size = 0
start_path = './MIREX_Backup'  # To get size of current directory
for path, dirs, files in os.walk(start_path):
    for f in files:
        fp = os.path.join(path, f)
        total_size += os.path.getsize(fp)
print("Directory size: " + str(total_size))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13202710126350486, 'fraction_numerical': 0.016480498077275224, 'mean_word_length': 2.7487989018531227, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '407084', 'license': 'MIT', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1630, 'n_words': 400, 'path': 'Auto_Backup/Auto_Backup.ipynb', 'repo_name': 'SharmaTanish/Amazing-Python-Scripts', 'size': '5461'}