Python实现的文件夹同步

一直在找好的文件同步软件,在笔记本和台式机之间。用起来都不是太满意,何不自己写一个呢?不成章法,有空再改,先实现功能,用起来。
#-*-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!') 

 

你可能感兴趣的:(哈哈编程)