Python FTP兩個(gè)文件夾間的同步實(shí)例代碼
具體代碼如下所示:
# -*- coding: utf-8 -*-
'''''''
ftp自動(dòng)檢測源文件夾的更新,將源文件夾更新的內(nèi)容拷貝到目標(biāo)文件夾中
使用樹的層序遍歷算法,支持深度目錄拷貝
'''
import os
from ftplib import FTP
import os,sys,string,datetime,time
import shutil
import socket
class MyUpdateMonitor(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()
# 源文件文件隊(duì)列
self.FolderListOld = []
# 目標(biāo)文件文件隊(duì)列
self.FolderListNew = []
def __del__(self):
self.ftp.close()
self.FolderListOld.clear()
self.FolderListNew.clear()
def FtpLogin(self):
ftp = self.ftp
try:
timeout = 300
socket.setdefaulttimeout(timeout)
ftp.set_pasv(True)
print u'開始連接到 %s' %(hostaddr)
ftp.connect(hostaddr)
print u'成功連接到 %s' %(hostaddr)
print u'開始登錄到 %s' %(hostaddr)
ftp.login(username, password)
print u'成功登錄到 %s' %(hostaddr)
ftp.getwelcome()
except Exception, e:
print 'find exception now:',e
# 使用樹的層序遍歷來檢查文件目錄
def LevelOrderFolder(self):
# 新增文件起始位置和終止位置
start = 0
end = 0
try:
# 將根目錄放入隊(duì)列中
self.FolderListOld.append(self.remotedir_old)
self.FolderListNew.append(self.remotedir_new)
while not (0 == len(self.FolderListOld)):
end = start
# 將文件夾下的文件全部壓入隊(duì)列
if os.path.isdir(self.FolderListOld[0]):
files = os.listdir(self.FolderListOld[0])
for file in files:
self.FolderListOld.append(os.path.join(self.FolderListOld[0], file))
# 確定新增文件在隊(duì)列中的位置
end += len(files)
# 將已經(jīng)查看的文件夾刪除
del self.FolderListOld[0]
# 檢查目標(biāo)文件夾該級(jí)目錄
if os.path.isdir(self.FolderListNew[0]):
# 將該級(jí)目錄的文件都列出
files = os.listdir(self.FolderListNew[0])
# 檢查源文件該級(jí)目錄下的文件在目標(biāo)該級(jí)目錄下是否有
for file in self.FolderListOld[start:end]:
temp = file.split('\\')
if temp[-1] in files:
# 這里判斷文件大小是否一致,不一致拷過去
if os.path.isfile(file) and not os.path.getsize(file) == os.path.getsize(os.path.join(self.FolderListNew[0], temp[-1])):
print 'Find the file(%s) size is changed!\n' % file
# print r'Start delete...\n'
# os.remove(os.path.join(self.FolderListNew[0], temp[-1]))
# print r'delete is over...\n'
print 'Start Copy...\n'
shutil.copyfile(file, os.path.join(self.FolderListNew[0], temp[-1]))
print 'Copy is over...\n'
# # 如果是文件夾存在,但是修改過,沒有必要全部拷貝文件夾,可以到文件夾中拷貝單個(gè)文件
# if os.path.isfile(file) and not (os.path.getmtime(file) == os.path.getmtime(os.path.join(self.FolderListNew[0], temp[-1]))):
# print 'Find the file(%s) size is changed!\n' % file
# changetime = os.path.getmtime(file) #以毫秒為單位的時(shí)間,自1970年開始到現(xiàn)今
# changetime = float(changetime)
# print 'Change Time is', time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(changetime)), r'\n'
#
# print 'Start Copy...\n'
# shutil.copyfile(file, os.path.join(self.FolderListNew[0], temp[-1]))
# print 'Copy is over...\n'
else:
if os.path.isdir(file):
# 如果是文件夾不存在使用,目錄樹拷貝
print 'Find the folder(%s) is updated!\n' % file
print 'Start Copy...\n'
shutil.copytree(file, os.path.join(self.FolderListNew[0], temp[-1]))
print 'Copy is over...\n'
else:
# 如果是文件
print 'Find the file(%s) is updated!\n' % file
print 'Start Copy...\n'
shutil.copyfile(file, os.path.join(self.FolderListNew[0], temp[-1]))
print 'Copy is over...\n'
self.FolderListNew.append(os.path.join(self.FolderListNew[0], temp[-1]))
del self.FolderListNew[0]
start = end - 1
except Exception, e:
print 'find exception now:',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()
while True:
print 'Start Check Update...\n'
monitorfileupdae.LevelOrderFolder()
print 'Check Update is Over...\tSleep one hour...'
time.sleep(3600)
print 'hello'
總結(jié)
以上所述是小編給大家介紹的Python FTP兩個(gè)文件夾間的同步實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Python自動(dòng)化辦公之Excel拆分與自動(dòng)發(fā)郵件
這篇文章主要何大家分享一個(gè)真實(shí)的自動(dòng)化辦公案例,即向用戶發(fā)送帶有Excel附件的電子郵件,同時(shí)必須按用戶從主Excel文件中拆分?jǐn)?shù)據(jù)以創(chuàng)建他們自己的特定文件,然后將該文件通過電子郵件發(fā)送給正確的用戶,感興趣的可以了解一下2022-03-03
Python隊(duì)列Queue實(shí)現(xiàn)詳解
這篇文章主要介紹了Python隊(duì)列Queue實(shí)現(xiàn)詳解,隊(duì)列是一種列表,隊(duì)列用于存儲(chǔ)按順序排列的數(shù)據(jù),隊(duì)列是一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),不同的是隊(duì)列只能在隊(duì)尾插入元素,在隊(duì)首刪除元素,需要的朋友可以參考下2023-07-07
Python基于Socket實(shí)現(xiàn)簡易多人聊天室的示例代碼
這篇文章主要介紹了Python基于Socket實(shí)現(xiàn)簡易多人聊天室的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
pytorch實(shí)現(xiàn)用CNN和LSTM對(duì)文本進(jìn)行分類方式
今天小編就為大家分享一篇pytorch實(shí)現(xiàn)用CNN和LSTM對(duì)文本進(jìn)行分類方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
在Python中操作字典之setdefault()方法的使用
這篇文章主要介紹了在Python中操作字典之setdefault()方法的使用,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05
Python對(duì)兩個(gè)有序列表進(jìn)行合并和排序的例子
這篇文章主要介紹了Python對(duì)兩個(gè)有序列表進(jìn)行合并和排序的例子,最終代碼經(jīng)過不斷優(yōu)化,小編非常滿意,需要的朋友可以參考下2014-06-06
Python繪制可以表示點(diǎn)密度的散點(diǎn)圖得方法
本文介紹基于Python語言的matplotlib模塊,對(duì)Excel表格文件中的指定數(shù)據(jù),加以密度散點(diǎn)圖繪制的方法,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-05-05

