c++ Python -单向同步两个文件夹



我有一个Taks,从这个步骤有两个问题:

1。不能写日志文件,因为只写一行,循环的第一步。

2。不能更新动作时间->print("Last time updated at: =", CurrentData)//这里有一个问题,没有在循环中更新时间

请编写一个同步源和副本两个文件夹的程序。的程序应在复制文件夹中保存源文件夹的完整、相同的副本。用以下其中一种编程语言编写程序来解决测试任务:

Pythonc++

同步必须是单向的:后同步的内容应修改副本文件夹,使其与源文件的内容完全匹配文件夹;

定时同步。文件创建/复制/删除操作应记录到文件和控制台输出;

在此捍卫我的生命!

import os
import shutil
from time import time, sleep
from datetime import datetime


print('Synchronization will be done every 10 seconds')

#here we ask location of file from where we want to copy files.
print('Please write Location of main Folder, Example:D:FolderOne\')
source_folder=input('')
#here we ask location of folder where we want to paste files.
print('Please write Location of replica Folder, Example:D:FolderTwo\')
#destination_folder=input('')
now = datetime.now()
CurrentData= now.strftime("%d/%m/%Y")
while True:
for file_name in os.listdir(source_folder):
source = source_folder + file_name
destination = destination_folder + file_name
if os.path.isfile(source):
shutil.copy(source, destination)
print('File ', file_name, ' has moved from: ',source_folder, ' to: ',destination_folder)
f = open("D:log.txt", "w")
writingText='File '+file_name+' has moved from: '+source_folder+' to: '+destination_folder+CurrentData
f.write(writingText)
//Second problem is here, cant write in log.txt file full loop information

print("Last time updated at: =", CurrentData) //one problem is here, not updating time in loop
sleep(60 - time() % 60)
os.system('cls')

在程序开始时,您可以将源的所有内容复制到目标,稍后您可以使用模块watchdog来监视源中生成的事件并根据事件采取行动(您也可以将此事件记录到日志文件中),因此您不必每次都从源复制所有内容。

https://pypi.org/project/watchdog/

最新更新