How to make periodical synchronization in this function?

Question:

I need to make an app that sends its content to another directory so their contents are exactly the same. On top of that I need it to send logs and also I should provide an amount of time when this application makes another synchronization between the folders. Atm it works only once and I got an error message that I need to provide sys.args (path1, path2 etc) once again.

from subprocess import call
import sys
import os
import glob
import filecmp
import schedule
import time


def check(path1, path2, path3):
    comp = filecmp.cmp(path1, path2, shallow=True)
    # Comparison of files on two folders, if they aren't the same the replica folder's content is removed and then the
    # content of the source file is copied here.
    if comp is True:
        pass
        print("Content of both folder is the same!", file=open(path3, 'a'))
    else:
        files = glob.glob(path2 + "\" + '*')
        for f in files:
            os.remove(f)
    print("Files from: " + path1 + " sent to: " + path2, file=open(path3, 'a'))
    try:
        call(["robocopy", path1, path2, "/MIR"])
        print("The content of both libraries is now the same!", file=open(path3, 'a'))
    finally:
        pass


def s(interval):  # s stands for schedule
    schedule.every(int(interval)).minutes.do(check)
    while True:
        schedule.run_pending()
        time.sleep(1)


if sys.argv[1] == "-c":
    check(sys.argv[2], sys.argv[3], sys.argv[5])
    s(sys.argv[4])
# Given are 5 arguments when calling this function via cmd: 1 -c for calling it, 2 is path/to/source, 3 path/to/replica
# 4 - time how often this app should run once again(in minutes) and 5 is the path where log file is going to be placed.
Asked By: Norbert Wieczorek

||

Answers:

The only reason this is working once, is because you are calling the check function, outside of the scheduler:

check(sys.argv[2], sys.argv[3], sys.argv[5])

As the error message should indicate, you haven’t passed any argument to the function that is called by the scheduler. To do so, according to the documentation, you can simply pass the required argument to the do function, which will forward them to your function. In your case, that would look like

def s(interval, path1, path2, path3):  # s stands for schedule
    schedule.every(int(interval)).minutes.do(check, path1=path1, path2=path2, path3=path3)
    while True:
        schedule.run_pending()
        time.sleep(1)

if sys.argv[1] == "-c":
    check(sys.argv[2], sys.argv[3], sys.argv[5])
    s(sys.argv[4], path1=sys.argv[2], path2=sys.argv[3], path3=sys.argv[5])

for instance.

Answered By: Florent Monin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.