Python FTP两个文件夹间的同步实例代码
具体代码如下所示:
#-*-coding:utf-8-*-
'''''''
ftp自动检测源文件夹的更新,将源文件夹更新的内容拷贝到目标文件夹中
使用树的层序遍历算法,支持深度目录拷贝
'''
importos
fromftplibimportFTP
importos,sys,string,datetime,time
importshutil
importsocket
classMyUpdateMonitor(object):
def__init__(self,hostaddr,username,password,remotedir_old,remotedir_new,port=21):
self.hostaddr=hostaddr
self.username=username
self.password=password
self.remotedir_old=remotedir_old
self.remotedir_new=remotedir_new
#self.port=port
self.ftp=FTP()
#源文件文件队列
self.FolderListOld=[]
#目标文件文件队列
self.FolderListNew=[]
def__del__(self):
self.ftp.close()
self.FolderListOld.clear()
self.FolderListNew.clear()
defFtpLogin(self):
ftp=self.ftp
try:
timeout=300
socket.setdefaulttimeout(timeout)
ftp.set_pasv(True)
printu'开始连接到%s'%(hostaddr)
ftp.connect(hostaddr)
printu'成功连接到%s'%(hostaddr)
printu'开始登录到%s'%(hostaddr)
ftp.login(username,password)
printu'成功登录到%s'%(hostaddr)
ftp.getwelcome()
exceptException,e:
print'findexceptionnow:',e
#使用树的层序遍历来检查文件目录
defLevelOrderFolder(self):
#新增文件起始位置和终止位置
start=0
end=0
try:
#将根目录放入队列中
self.FolderListOld.append(self.remotedir_old)
self.FolderListNew.append(self.remotedir_new)
whilenot(0==len(self.FolderListOld)):
end=start
#将文件夹下的文件全部压入队列
ifos.path.isdir(self.FolderListOld[0]):
files=os.listdir(self.FolderListOld[0])
forfileinfiles:
self.FolderListOld.append(os.path.join(self.FolderListOld[0],file))
#确定新增文件在队列中的位置
end+=len(files)
#将已经查看的文件夹删除
delself.FolderListOld[0]
#检查目标文件夹该级目录
ifos.path.isdir(self.FolderListNew[0]):
#将该级目录的文件都列出
files=os.listdir(self.FolderListNew[0])
#检查源文件该级目录下的文件在目标该级目录下是否有
forfileinself.FolderListOld[start:end]:
temp=file.split('\\')
iftemp[-1]infiles:
#这里判断文件大小是否一致,不一致拷过去
ifos.path.isfile(file)andnotos.path.getsize(file)==os.path.getsize(os.path.join(self.FolderListNew[0],temp[-1])):
print'Findthefile(%s)sizeischanged!\n'%file
#printr'Startdelete...\n'
#os.remove(os.path.join(self.FolderListNew[0],temp[-1]))
#printr'deleteisover...\n'
print'StartCopy...\n'
shutil.copyfile(file,os.path.join(self.FolderListNew[0],temp[-1]))
print'Copyisover...\n'
##如果是文件夹存在,但是修改过,没有必要全部拷贝文件夹,可以到文件夹中拷贝单个文件
#ifos.path.isfile(file)andnot(os.path.getmtime(file)==os.path.getmtime(os.path.join(self.FolderListNew[0],temp[-1]))):
#print'Findthefile(%s)sizeischanged!\n'%file
#changetime=os.path.getmtime(file)#以毫秒为单位的时间,自1970年开始到现今
#changetime=float(changetime)
#print'ChangeTimeis',time.strftime('%Y-%m-%d%H:%M:%S',time.gmtime(changetime)),r'\n'
#
#print'StartCopy...\n'
#shutil.copyfile(file,os.path.join(self.FolderListNew[0],temp[-1]))
#print'Copyisover...\n'
else:
ifos.path.isdir(file):
#如果是文件夹不存在使用,目录树拷贝
print'Findthefolder(%s)isupdated!\n'%file
print'StartCopy...\n'
shutil.copytree(file,os.path.join(self.FolderListNew[0],temp[-1]))
print'Copyisover...\n'
else:
#如果是文件
print'Findthefile(%s)isupdated!\n'%file
print'StartCopy...\n'
shutil.copyfile(file,os.path.join(self.FolderListNew[0],temp[-1]))
print'Copyisover...\n'
self.FolderListNew.append(os.path.join(self.FolderListNew[0],temp[-1]))
delself.FolderListNew[0]
start=end-1
exceptException,e:
print'findexceptionnow:',e
if__name__=='__main__':
#配置如下变量
hostaddr=r'10.204.16.28'#ftp地址
username=r''#用户名
password=r''#密码
remotedir_old=r'\\10.204.16.28\Home\TDME\Test\Old\TMUMH_1.6.1055'
remotedir_new=r'\\10.204.16.28\Home\TDME\Test\New\TMUMH_1.6.1055'
monitorfileupdae=MyUpdateMonitor(hostaddr,username,password,remotedir_old,remotedir_new)
monitorfileupdae.FtpLogin()
whileTrue:
print'StartCheckUpdate...\n'
monitorfileupdae.LevelOrderFolder()
print'CheckUpdateisOver...\tSleeponehour...'
time.sleep(3600)
print'hello'
总结
以上所述是小编给大家介绍的PythonFTP两个文件夹间的同步实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!