双系统文档备份

#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os
import time
import datetime
import csv
import filecmp

def get_FileList(file_path):#获取文件列表
	files= os.listdir(file_path)
	
	sumList = []
	for file in files:
		try:
			os.listdir(file_path+"/"+file)
		except IOError:
			sumList.append(file_path+"/"+file)
		else:
			#print(file_path+file)
			sumList.extend(get_FileList(file_path+file+"/"))
	return sumList
	
def write_FileListToCSV(fileDict,filename):#内容为 文件名#日期
	with open(filename,'w') as f:
		ff = csv.writer(f)
		for k in fileDict.keys():
			ff.writerows([k])

def updateList(file_path,fileName):#更新文件列表
	files= os.listdir(file_path)
	sumList = []
	sumList.extend(get_FileList(file_path))
	return sumList

def uploadFile(linPath,winPath,newList):#更新文件
	for i in newList:
		theTime = datetime.datetime.now()
		try:
			os.system("cp -f --path \""+i+"\" "+winPath)
		except IOError:
			os.system("echo  '[ERROR]\t"+str(theTime)+"\t"+i+"'>>"+winPath+"log.log")
		else:
			os.system("echo  '[SUCCESS]\t"+str(theTime)+"\t"+i+"'>>"+winPath+"log.log")
			
def compareLists(linDict):#查找待更新列表
	newList=[]
	for k in linDict:
		if os.path.exists("/home/***/F/文档/Linux/"+str(k)):
			if filecmp.cmp(str(k),"/home/kwzc4/disk/F/F/文档/Linux/"+str(k)):
				continue
			newList.append(k)
		else:
			newList.append(k)
	return newList


if __name__=="__main__":
	#init
	linPath = "/home/xxx/文档/"
	winPath = "/home/***/F/文档/Linux/"
	linCsvName = "/home/xxx/app/.linFile"

	linList = updateList(linPath,linCsvName)
	print(linList)
	pastTime = datetime.datetime.now()
	while True:
		now = datetime.datetime.now()
		time.sleep(4)
		#print(str(now)+str(now.second-pastTime.second))
		if int((now-pastTime).seconds) > 10:
			print(str(pastTime)+"\t"+str(now)+"\t"+str(int((now-pastTime).seconds)))
			linList = updateList(linPath,linCsvName)
			newList = compareLists(linList)
			print(newList)
			pastTime = now
			if len(newList):
				uploadFile(linPath,winPath,newList)
				pastTime = now

 

注:1.文中的***和xxx根据自己计算机进行设置;

2.扫描时间根据计算机性能自行设置;

3.资源分配待优化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值