Pythonによるフォルダ同期
2251 ワード
, 。 , ? , , , 。
#-*-coding:utf-8-*-
import os
remotePath = "\\\\192.168.1.xxx\\share"
localPath = "F:\\share"
fileList = os.walk(remotePath)
dictFile = {}
lenPath = len(remotePath)
for file in fileList:
for filename in file[2]:
absPath = file[0] + "\\" + filename
relPath = absPath[lenPath:]
srcFilename = remotePath + relPath
desFilename = localPath + relPath
if os.path.isfile(desFilename):
desTime = os.path.getmtime(desFilename)
else:
desTime = 0
srcTime = os.path.getmtime(srcFilename)
dictFile[srcFilename] = [desFilename,srcTime,desTime]
fileList = os.walk(localPath)
lenPath = len(localPath)
for file in fileList:
for filename in file[2]:
absPath = file[0] + "\\" + filename
relPath = absPath[lenPath:]
srcFilename = remotePath + relPath
desFilename = localPath + relPath
if os.path.isfile(srcFilename):
srcTime = os.path.getmtime(srcFilename)
else:
srcTime = 0
desTime = os.path.getmtime(desFilename)
dictFile[srcFilename] = [desFilename,srcTime,desTime]
for srcFilename in dictFile.keys():
data = dictFile[srcFilename]
desFilename = data[0]
srcTime = data[1]
desTime = data[2]
print(srcFilename," ",desFilename, " ", srcTime, " ", desTime, " ")
if srcTime == desTime:
print("%s no update" % (desFilename[lenPath:]))
elif srcTime > desTime:
if not os.path.isdir(os.path.split(desFilename)[0]):
os.system("mkdir " + os.path.split(desFilename)[0])
copyCommand = 'copy %s %s'%(srcFilename, os.path.split(desFilename)[0])
print ('copyCommand: '+copyCommand)
if os.system(copyCommand) == 0:
print ('copy successed!')
else:
print ('copy failed!')
else:
if not os.path.isdir(os.path.split(srcFilename)[0]):
os.system("mkdir " + os.path.split(srcFilename)[0])
copyCommand = 'copy %s %s'%(desFilename, os.path.split(srcFilename)[0])
print ('copyCommand: '+copyCommand)
if os.system(copyCommand) == 0:
print ('copy successed!')
else:
print ('copy failed!')