#!/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