目录文件备份工具
来源:互联网 发布:java calendar api 编辑:程序博客网 时间:2024/06/06 04:22
# -*- coding: cp936 -*-
'''这个程序实现了备份目录的功能,
演示了如下
1)shutil模块中copy/copytree的使用方法;
2)os.path模块isfile/isdir/join的使用方法
3)递归方法使用
'''
import shutil
import os
def backupDir(pathsrc,pathdest):
'''备份目录pathsrc到pathdest
实现方法:
查找源目录中的所有目录和文件,对每一项进行比较:
1)如果源目录中的item不存在,直接复制到目标目录即可
2)如果源目录中的item是目录,则进行递归调用
3)如果源目录中的item是文件,比较的修改日期,如果源文件比目标文件新,则复制文件,否则不予处理
4) 不是目录或文件,直接复制,不再进行其他的判断
'''
if os.path.isdir(pathsrc):
os.chdir(pathsrc)
for item in os.listdir(pathsrc):
itemsrc = os.path.join(pathsrc,item)
itemdest = os.path.join(pathdest,item)
# 先判断文件或目录是否存在,如果目标目录中没有这个文件或目录,直接复制即可
if os.path.exists(itemdest):
# 如果是目录,进行递归调用
if os.path.isdir(itemsrc):
backupDir(itemsrc,itemdest)
# 如果是文件,进行修改时间的比较,若修改时间变化,覆盖目标文件
elif os.path.isfile(itemsrc):
if os.path.exists(itemdest):
if os.stat(itemsrc)[-2] > os.stat(itemdest)[-2]:
shutil.copy(itemsrc,itemdest)
else:
# 其他情况直接复制,不论是否改变
shutil.copy(itemsrc,itemdest)
else:
# 如果不存在,复制整个目录或文件
if os.path.isdir(itemsrc):
shutil.copytree(itemsrc,itemdest)
elif os.path.isfile(itemsrc):
shutil.copy(itemsrc,itemdest)
os.chdir('..')
return
else:
return
# backupDir('c:/1','c:/2')
from Tkinter import *
root = Tk()
# 创建源目录
Label(root,text = "source directory:").grid(row = 0,column = 0,stick = W)
vSource = StringVar()
vSource.set('c:/1')
Entry(root,textvariable = vSource).grid(row = 0,column = 1)
# 创建目标目录
Label(root,text = "destination directory:").grid(row = 1,column = 0,stick = W)
vDestination = StringVar()
vDestination.set('c:/2')
Entry(root,textvariable = vDestination).grid(row = 1,column = 1)
# 创建备份按钮
Button(root,text = 'start backup directory',
command = lambda arg1 = vSource.get(),arg2 = vDestination.get():backupDir(arg1,arg2)
).grid(row = 2,column = 0,columnspan = 2,stick = E + W + N + S)
root.mainloop()
'''这个程序实现了备份目录的功能,
演示了如下
1)shutil模块中copy/copytree的使用方法;
2)os.path模块isfile/isdir/join的使用方法
3)递归方法使用
'''
import shutil
import os
def backupDir(pathsrc,pathdest):
'''备份目录pathsrc到pathdest
实现方法:
查找源目录中的所有目录和文件,对每一项进行比较:
1)如果源目录中的item不存在,直接复制到目标目录即可
2)如果源目录中的item是目录,则进行递归调用
3)如果源目录中的item是文件,比较的修改日期,如果源文件比目标文件新,则复制文件,否则不予处理
4) 不是目录或文件,直接复制,不再进行其他的判断
'''
if os.path.isdir(pathsrc):
os.chdir(pathsrc)
for item in os.listdir(pathsrc):
itemsrc = os.path.join(pathsrc,item)
itemdest = os.path.join(pathdest,item)
# 先判断文件或目录是否存在,如果目标目录中没有这个文件或目录,直接复制即可
if os.path.exists(itemdest):
# 如果是目录,进行递归调用
if os.path.isdir(itemsrc):
backupDir(itemsrc,itemdest)
# 如果是文件,进行修改时间的比较,若修改时间变化,覆盖目标文件
elif os.path.isfile(itemsrc):
if os.path.exists(itemdest):
if os.stat(itemsrc)[-2] > os.stat(itemdest)[-2]:
shutil.copy(itemsrc,itemdest)
else:
# 其他情况直接复制,不论是否改变
shutil.copy(itemsrc,itemdest)
else:
# 如果不存在,复制整个目录或文件
if os.path.isdir(itemsrc):
shutil.copytree(itemsrc,itemdest)
elif os.path.isfile(itemsrc):
shutil.copy(itemsrc,itemdest)
os.chdir('..')
return
else:
return
# backupDir('c:/1','c:/2')
from Tkinter import *
root = Tk()
# 创建源目录
Label(root,text = "source directory:").grid(row = 0,column = 0,stick = W)
vSource = StringVar()
vSource.set('c:/1')
Entry(root,textvariable = vSource).grid(row = 0,column = 1)
# 创建目标目录
Label(root,text = "destination directory:").grid(row = 1,column = 0,stick = W)
vDestination = StringVar()
vDestination.set('c:/2')
Entry(root,textvariable = vDestination).grid(row = 1,column = 1)
# 创建备份按钮
Button(root,text = 'start backup directory',
command = lambda arg1 = vSource.get(),arg2 = vDestination.get():backupDir(arg1,arg2)
).grid(row = 2,column = 0,columnspan = 2,stick = E + W + N + S)
root.mainloop()
- 目录文件备份工具
- 文件目录备份bat
- 文件备份好工具
- 简易文件备份工具
- 文件增量备份工具
- 文件自动备份工具
- 文件增量备份工具
- Python 备份指定目录文件
- 文件目录查看工具
- 文件同步备份工具Synkron
- 服务器文件自动备份工具
- suse linux 文件目录备份和恢复
- 文件目录结构导出工具
- emacs文件/目录比较工具
- linux下备份一个目录下所有文件及目录
- 打算写一个实时文件备份工具
- 简单文件备份工具(C#)
- 备份目录
- JavaScript 14页面停留时间
- MYSQL常见问题
- Second Life 的社群观点
- DLL学习小结
- AJAX+PHP+MYSQL实现两个下拉框间的数据联动
- 目录文件备份工具
- GeoTools学习(referencing)
- HR声音:应届毕业生,我对你“非常不满”
- 基于JSON格式报文的AJAX通用实现
- JavaScript 15 自动刷新页面
- Web 2.0 的迷思与真实,其三
- 金钱关系
- JavaScript DOM编程艺术读书笔记(2-2)
- 小圈圈