-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileSync.py
More file actions
83 lines (68 loc) · 2.52 KB
/
FileSync.py
File metadata and controls
83 lines (68 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import shutil
import time
import schedule
import logging
def startsync(timing):
schedule.every().day.at(timing).do(main)
while True:
schedule.run_pending()
time.sleep(300)
def main():
srcdir = "sourcedir"
desdir = "destinationdir"
sync = SyncFile(srcdir, desdir)
sync.syncdir()
class SyncFile:
def __init__(self, fromdir, todir):
self.fromdir = fromdir
self.todir = todir
def syncdir(self):
print("Start synchronize files from %s to %s at %s" % (self.fromdir, self.todir, self._localtime()))
self._copydir(self.fromdir, self.todir)
print("Today's synchronization has been finished!")
def _localtime(self):
localtime = time.asctime(time.localtime(time.time()))
return localtime
def _copydir(self, fromdir, todir):
self._mkdir(todir)
for filename in os.listdir(fromdir):
if filename.startswith('.'):
continue
elif filename.startswith('FileSync'):
continue
elif filename.startswith('venv'):
continue
elif filename.startswith("All Users"):
continue
fromfile = fromdir + os.sep + filename
tofile = todir + os.sep + filename
if os.path.isdir(fromfile):
self._copydir(fromfile, tofile)
else:
self._copyfile(fromfile, tofile)
def _copyfile(self, fromfile, tofile):
if not os.path.exists(tofile):
try:
shutil.copy2(fromfile, tofile)
logging.info("新增文件%s ==> %s at %s" % (fromfile, tofile, self._localtime()))
except PermissionError:
logging.info("文件%s 权限不够" % fromfile)
fromstat = os.stat(fromfile)
tostat = os.stat(tofile)
if fromstat.st_ctime > tostat.st_ctime:
try:
shutil.copy2(fromfile, tofile)
logging.info("更新文件%s ==> %s at %s" % (fromfile, tofile, self._localtime()))
except PermissionError:
logging.info("文件%s 权限不够" % fromfile)
def _mkdir(self, path):
path = path.strip()
path = path.rstrip(os.sep)
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
logging.info(path + ' 目录创建成功 at %s' % self._localtime())
if __name__ == '__main__':
logging.basicConfig(filename="FileSyncLog.log", level=logging.INFO)
startsync("05:00")