-
大小: 4KB文件類型: .py金幣: 1下載: 0 次發布日期: 2021-05-26
- 語言: Python
- 標簽:
資源簡介
本地兩個文件夾同步,新建、刪除、修改、移動位置、重命名、復制目錄或者文件等操作都可以實現,修改文件路徑即可用,watchdog思想編寫。項目設計涉及到這一塊,自己通過watchdog編寫了同步的代碼,希望可以給大家提供幫助。
代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
from?watchdog.observers?import?Observer
from?watchdog.events?import?*
import?time
import?sys
import?os.path?
import?shutil
import?re
import?urllib2
import?requests
reload(sys)
sys.setdefaultencoding(‘utf-8‘)
monipath=“/home/fyerd/test2“
syncpath=“/home/fyerd/test3“
class?FileEventHandler(FileSystemEventHandler):
????def?__init__(self):
????????FileSystemEventHandler.__init__(self)
????def?on_moved(self?event):
????????if?event.is_directory:
????????????print(“directory?moved?from?{0}?to?{1}“.format(event.src_pathevent.dest_path))
????????????newpath=event.dest_path
????????????oldpath=event.src_path
????????????newdir=newpath.replace(monipathsyncpath)
????????????olddir=oldpath.replace(monipathsyncpath)
????????????if?os.path.exists(olddir):
????????????????os.rename(olddirnewdir)
????????????else:
????????????????print(“pass“)
????????else:
????????????print(“file?moved?from?{0}?to?{1}“.format(event.src_pathevent.dest_path))
????????????a=event.src_path.split(“/“)[-1]
????????????if?re.match(‘.gou‘a[0:4])!=None:#xiugai?upload
????????????????xgpath=event.dest_path
????????????????xgdir=xgpath.replace(monipathsyncpath)
????????????????shutil.copyfile(event.dest_pathxgdir)
????????????else:
????????????????(filepath1filename1)?=?os.path.split(event.src_path)
????????????????(filepath2filename2)?=?os.path.split(event.dest_path)
????????????????print(filepath1filepath2)
????????????????#?re.match(filepath1filepath2)!=None:#chongmingming?and?yiwei
????????????????print(“move?file!!!!!“)
????????????????newpath=event.dest_path
????????????????oldpath=event.src_path
????????????????print(newpatholdpath)
????????????????newdir=newpath.replace(monipathsyncpath)
????????????????olddir=oldpath.replace(monipathsyncpath)
????????????????print(newdirolddir)
????????????????if?os.path.exists(olddir):
????????????????????os.rename(olddirnewdir)
?????
- 上一篇:圖像相似度計算python
- 下一篇:基于python的推薦系統庫
評論
共有 條評論