我需要制作一個將其內容發送到另一個目錄的應用程式,以便它們的內容完全相同。最重要的是,我需要它來發送日志,并且我應該提供一段時間,當這個應用程式在檔案夾之間進行另一個同步時。Atm 它只作業一次,我收到一條錯誤訊息,我需要再次提供 sys.args(path1、path2 等)。
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.
uj5u.com熱心網友回復:
這作業一次的唯一原因是因為您check在調度程式之外呼叫該函式:
check(sys.argv[2], sys.argv[3], sys.argv[5])
正如錯誤訊息應該指出的那樣,您沒有將任何引數傳遞給調度程式呼叫的函式。為此,根據檔案,您可以簡單地將所需的引數傳遞給do函式,該函式會將它們轉發給您的函式。在你的情況下,這看起來像
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])
例如。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513395.html
標籤:Python视窗同步
