Python同步文件

最近在做Python开发,研究了技术大牛写的脚本,在他的脚本上做了优化。优化脚本已在做过测试还是挺好用的,如果你觉得不错就直接拿到生产用吧。

先直接放代码出来:

---------------------------------------------------------------------------------------------------------

import re,shutil,os,sys,filecmp

diffFilesList = []

def commpare(dir1,dir2):

cmpobs=filecmp.dircmp(dir1,dir2)

dir1_only=cmpobs.left_only

dir1_diff=cmpobs.diff_files

[diffFilesList.append(os.path.join(os.path.abspath(dir1),a)) for a in dir1_diff]

[diffFilesList.append(os.path.join(os.path.abspath(dir1),a)) for a in dir1_only]

if len(cmpobs.common_dirs) > 0:

for a in cmpobs.common_dirs:

commpare(os.path.abspath(os.path.join(dir1,a)),os.path.abspath(os.path.join(dir2,a)))

return diffFilesList

def main():

if len(sys.argv) < 3:

print "Plese use %s sourcedir backdir" % sys.argv[0]

sys.exit()

else:

dir1=os.path.abspath(sys.argv[1])

dir2=os.path.abspath(sys.argv[2])

ifMakeDir = True

while ifMakeDir:

diffFilesList = []

destinationFiles = []

ifMakeDir=False

sourceFiles=commpare(dir1,dir2)

for a in sourceFiles:

destinationFile=re.sub(dir1,dir2,a)

destinationFiles.append(destinationFile)

if os.path.isdir(a):

if not os.path.exists(destinationFile):

os.makedirs(destinationFile)

print "Make dir %s" % destinationFile

ifMakeDir=True

destinationFiles = []

sourceFiles=[]

sourceFiles=commpare(dir1,dir2)

[ destinationFiles.append(re.sub(dir1,dir2,a)) for a in sourceFiles]

for a,b in zip(sourceFiles,destinationFiles):

if os.path.isfile(a):

print "Copy file %s to %s" % (a,b)

shutil.copyfile(a,b)

#print sourceFiles,destinationFiles

if __name__ == ‘__main__‘:

main()

---------------------------------------------------------------------------------------------------------

直接看效果:

[[email protected] tmp]# pwd

/tmp

[[email protected] tmp]# tree testsyncfile/

testsyncfile/

├── destinationfiles

└── sourcefiles

├── file

└── test1

├── file1

└── test2

├── file2

└── test3

5 directories, 3 files

[[email protected] tmp]# python syncfile.py /tmp/testsyncfile/sourcefiles testsyncfile/destinationfiles/

Make dir /tmp/testsyncfile/destinationfiles/test1

Make dir /tmp/testsyncfile/destinationfiles/test1/test2

Make dir /tmp/testsyncfile/destinationfiles/test1/test2/test3

Copy file /tmp/testsyncfile/sourcefiles/file to /tmp/testsyncfile/destinationfiles/file

Copy file /tmp/testsyncfile/sourcefiles/file to /tmp/testsyncfile/destinationfiles/file

Copy file /tmp/testsyncfile/sourcefiles/test1/file1 to /tmp/testsyncfile/destinationfiles/test1/file1

Copy file /tmp/testsyncfile/sourcefiles/file to /tmp/testsyncfile/destinationfiles/file

Copy file /tmp/testsyncfile/sourcefiles/test1/file1 to /tmp/testsyncfile/destinationfiles/test1/file1

Copy file /tmp/testsyncfile/sourcefiles/test1/test2/file2 to /tmp/testsyncfile/destinationfiles/test1/test2/file2

Copy file /tmp/testsyncfile/sourcefiles/file to /tmp/testsyncfile/destinationfiles/file

Copy file /tmp/testsyncfile/sourcefiles/test1/file1 to /tmp/testsyncfile/destinationfiles/test1/file1

Copy file /tmp/testsyncfile/sourcefiles/test1/test2/file2 to /tmp/testsyncfile/destinationfiles/test1/test2/file2

Copy file /tmp/testsyncfile/sourcefiles/file to /tmp/testsyncfile/destinationfiles/file

Copy file /tmp/testsyncfile/sourcefiles/test1/file1 to /tmp/testsyncfile/destinationfiles/test1/file1

Copy file /tmp/testsyncfile/sourcefiles/test1/test2/file2 to /tmp/testsyncfile/destinationfiles/test1/test2/file2

[[email protected] tmp]# tree testsyncfile/

testsyncfile/

├── destinationfiles

│   ├── file

│   └── test1

│       ├── file1

│       └── test2

│           ├── file2

│           └── test3

└── sourcefiles

├── file

└── test1

├── file1

└── test2

├── file2

└── test3

8 directories, 6 files

----------------------------------------------------------------------------------------------------------

简单说明下脚本:

简单说明下脚本,脚本对源数据与目的数据做对比,有差异的文件做替换和新增,对目录做新增。但对于目的数据多余的目录和文件,脚本不错处理。希望使用脚本的人能根据自己的场景灵活运用。

刚开始写博客,写的比较草率,后续将分享更加生动的文章!

对于这篇文章大家有疑问或者建议欢迎留言。

时间: 2024-07-31 16:35:24

Python同步文件的相关文章

python之文件对象

防伪码忘情公子著 文件对象是用来访问文件系统接口所对应的数据的 文件系统是OS用于明确磁盘或分区上的文件的方法和数据结构-即在磁盘上组织文件的方法 计算机文件或称文件.电脑档案.档案是存储在某种长期储存设备或临时存储设备中的一段数据流并且归属于计算机文件系统管理之下 概括来讲 文件是计算机中由OS管理的具有名字的存储区域 在Linux系统上文件被看做是字节序列 要想把数据存储到文件中有一个前提那就是必须序列化非序列化的数据是不能简单的存储在文件系统中的文件中的 对于python来说文件对象不仅可

Python读写文件

Python读写文件1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.txt')try:     all_the_text = file_object.read( )finally:     file_object.close( ) 注:不能把open语句放在try块里,因为当打开文件出现异常时,文件对象file_object无法执行close()方法.

解决Python读取文件时出现UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode byte...

用Python在读取某个html文件时会遇到下面问题: 出问题的代码: 1 if __name__ == '__main__': 2 fileHandler = open('../report.html', mode='r') 3 4 report_lines = fileHandler.readlines() 5 for line in report_lines: 6 print(line.rstrip()) 修改方式是在open方法指定参数encoding='UTF-8': if __nam

将Python脚本文件包装成可执行文件

将Python脚本文件包装成可执行文件,其目的有二: 一则: 不需要依赖Python编译器就可以运行软件 二则: 不想让自己的源码公布出去 常用的工具有: py2exe.cx_freeze等 [工具:py2exe] 安装py2exe 安装该工具很简单: 只需要从官方网站:http://www.py2exe.org/下载与版本对应的安装程序,点击下一步即可完成安装. 安装后,执行import py2exe,不报错则表示安装成功! >>> import py2exe >>>

[改]在windows右键菜单中加入“新建Python File文件”并创建模板

1.首先写好模板文件,随便保存在一个地方,比如我是"D:\Python27\foo.py"; 2.打开注册表(regedit),找到 [HKEY_CLASSES_ROOT] -> [.py] (没有的话,自己新建项.py); 3.在 [.py] 下新建项 [ShellNew] (已经有的话就删掉重建); 4.在 [ShellNew] 下新建 字符串值 ,名称为 FileName ,键值为模板文件的绝对路径,比如我的是 D:\Python27\foo.py ; 在右键新建菜单中就会

Python open文件读写模式说明

对于Python打开文件的模式,总是记不住,这次在博客里记录一下 r+: Open for reading and writing.  The stream is positioned  at  the beginning of the file. w+:Open for reading and writing.  The file is created  if  it  does not  exist, otherwise it is truncated.  The stream is pos

从自动生成.h的头文件集合和类声明集合到用python读写文件

最近在用python自动生成c++的类.因为这些类会根据需求不同产生不同的类,所以需要用python自动生成.由于会产生大量的类,而且这些类是变化的.所以如果是在某个.h中要用include来加载这些类,会累死人的.所以用python来生成这些类的头文件引用和类的类名声明 先看例子,再聊python的读写文件的代码 在聊聊我的python代码 ------------------------> 好吧.上面的图就是面临的需求 下面来聊聊从网上找的读写文件的python代码吧.csdn的一个博主写的

python之文件操作-复制、剪切、删除等

下面是把sourceDir文件夹下的以.JPG结尾的文件全部复制到targetDir文件夹下: <span style="font-size:18px;">>>>import os >>> import os.path >>> import shutil >>> def copyFiles(sourceDir,targetDir): for files in os.listdir(sourceDir):

Python Selenium 文件上传(二)

今天补充一种文件上传的方法 主要是因为工作中使用SendKeys方法不稳定,具体方法见: Python Selenium 文件上传(一) 这种方法直接通过命令行执行脚本时没有问题,可以成功上传,但是如果通过saltstack 远程控制执行时,SendKeys就定位不到窗口了. 所以采用这种新的方式来实现文件上传功能,并完美的解决了这个问题. 具体操作步骤如下: 1.下载工具 AutoIt及使用 AutoIt目前最新是v3版本,这是一个使用类似BASIC脚本语言的免费软件,它设计用于Windows