日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python实现文件夹增量同步

發(fā)布時間:2024/4/14 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python实现文件夹增量同步 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

工作中,經(jīng)常要把windows的文件夾同步到linux上。xftp也可以,sublime也有遠程上傳的插件,但沒找到支持增量的。。。大量時間花在找插件,裝環(huán)境。。。然后一怒之下,自己東拼西湊了一下。

支持:上傳文件夾,和刪除遠程文件。

增量是用當前時間和上次上傳時間對比實現(xiàn)的。

cache.dat記錄每個文件的上傳時間,必要的時候可以刪除重新上傳

環(huán)境。python2.7

#coding=utf-8 #!/usr/bin/python import os import os.path import shutil import sys import string import fnmatch import pickle import time from ftplib import FTP import paramiko import socket from stat import S_ISDIRimport _cffi_backend import logginglogging.basicConfig()cachefilename="cache.dat"class SSHSession(object):def __init__(self,hostname,port,username='root',password=None,key_file=None):# # Accepts a file-like object (anything with a readlines() function) # in either dss_key or rsa_key with a private key. Since I don't # ever intend to leave a server open to a password auth.# self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)self.sock.connect((hostname,port))print("connect ", hostname, port)self.t = paramiko.Transport(self.sock)self.t.start_client()# supposed to check for key in keys, but I don't much care right now to find the right notationif password is not None:self.t.auth_password(username,password,fallback=False)else: raise Exception('Must supply either key_file or password')self.sftp=paramiko.SFTPClient.from_transport(self.t)try:l = pickle.load(open(cachefilename))except IOError:l = []self.db = dict(l)def put(self,localfile,remotefile):# Copy localfile to remotefile, overwriting or creating as needed.remotefile = remotefile.replace('\\', '/')print("upload %s %s"%(localfile, remotefile))try:self.sftp.put(localfile,remotefile)except Exception as e:print "put ", edef put_all(self,localpath,remotepath,ignore_list):self.mkdir_ifnotexists(remotepath)if os.path.exists(localpath):for root, dirs, files, in os.walk(localpath):for file in files:filename = os.path.join(root, file)if any(fnmatch.fnmatch(filename, pattern) for pattern in ignore_list):print 'Ignore', filenamecontinuemtime = time.ctime(os.path.getmtime(filename))if self.db.get(filename, None) != mtime:remotefile=os.path.join(remotepath,filename)self.mkdir_ifnotexists(os.path.dirname(remotefile))self.put(filename, remotefile)self.db[filename] = mtime# delete filesfor filename, value in self.db.items():if os.path.exists(filename) == False:remotefile=os.path.join(remotepath,filename)try:self.sftp.remove(remotefile)except Exception as e:print 'Delete',filename, epickle.dump(self.db.items(), open(cachefilename, "w"))print("ok!!!")def mkdir_ifnotexists(self, remotedirectory):remotedirectory = remotedirectory.replace('\\', '/')print "mkdir_ifnotexists", remotedirectorytry:self.sftp.chdir(remotedirectory) # Test if remote_path existsexcept IOError:self.mkdir_p(remotedirectory)def mkdir_p(self, remotedirectory):"""Change to this directory, recursively making new folders if needed.Returns True if any folders were created."""if remotedirectory == '/':# absolute path so change directory to rootself.sftp.chdir('/')returnif remotedirectory == '':# top-level relative directory must existreturntry:self.sftp.chdir(remotedirectory) # sub-directory existsexcept IOError:dirname, basename = os.path.split(remotedirectory.rstrip('/'))self.mkdir_p(dirname) # make parent directoriesself.sftp.mkdir(basename) # sub-directory missing, so created it self.sftp.chdir(basename)print "mkdir", basenamereturn Truedef iter_find_files(path, fnexp):for root, dirs, files, in os.walk(path):for filename in fnmatch.filter(files, fnexp):yield os.path.join(root, filename)def main():host="10.10.XX.XX"user="root"password="123"port=22localpath="./"remotepath="/data/server/remotefolder"ignore_list=['*svn/*','*.py']xfer = SSHSession(host,port,user,password)xfer.put_all(localpath, remotepath, ignore_list)if __name__ == '__main__':main()os.system("pause")

使用時填寫下(遠程地址,端口,用戶,密碼,本地路徑,遠程路徑,以及過濾文件)

轉(zhuǎn)載于:https://www.cnblogs.com/duzib/p/8577533.html

總結(jié)

以上是生活随笔為你收集整理的python实现文件夹增量同步的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。