Python实现备份文件实例
作者:shichen2014 时间:2022-01-03 11:10:05
本文实例讲述了Python实现备份文件的方法,是一个非常实用的技巧。分享给大家供大家参考。具体方法如下:
该实例主要实现读取一个任务文件, 根据指定的任务参数自动备份.
任务文件的格式: (注意,分号后面注释是不支持的)
[task] ; 一项任务开始
dir=h:/Project ; 指定备份的目录
recusive=1 ; 是否递归子目录
suffix=h|cpp|hpp|c|user|filters|vcxproj|sln|css|gif|html|bmp|png|lib|dsw|dsp|htm|html|ico|ini|jpg|rc|vssscc ; 指定备份的扩展名
exclude=0 ; 指定是备份上面的参数指定的扩展名还是排除指定的扩展名
zip=Project.zip ; 备份后的文件路径名
python代码如下:
# -*- coding: utf-8 -*-
import sys
import os
import zipfile
class Task:
#dir str directory
#bsub BOOL include subdirectory
#sfx str postsuffix ,sepeated by '|'
#ecld BOOL include or execlude the postsuffix sfx
def __init__(self,dir,bsub,sfx,ecld,zip):
self.dir = dir
self.bsub = bsub
self.suffix = sfx.split("|")
self.exclude = ecld
self.zip = zip
@staticmethod
def isfilter(sfx,sfxs,bexcld):
bFound = False
for e in sfxs:
if e == sfx:
bFound = True
break
if bexcld:
return not bFound;
else:
return bFound;
class QBackup:
'''备份指定目录下具备指定扩展名的文件'''
def __init__(self):
self._list = []
def __del__(self):
pass
#tfile 任务文件
def ReadTask(self,tfile):
dir = ""
bsub = False
sfx = ""
becld = False
zip = ""
try:
f = open(tfile,'r')
while True:
line = f.readline()
if len(line) == 0:
break;
line = line.strip(" ")
if "[Task]/n".lower() == line.lower():
# 读取接下来的4行
iline = 1
while iline <= 5:
line = f.readline()
line = line.strip(" /t/n") # 去除前后的空白符
idx = line.find("=")
if -1 == idx:
break;
atti = line[0:idx]
value = line[idx+1:]
print(value)
if "dir" == atti:
dir = value
elif "recusive" == atti:
bsub = bool(int(value))
elif "suffix" == atti:
sufix = value
elif "exclude" == atti:
becld = bool(int(value))
elif "zip" == atti:
zip = value
else:
break
iline += 1
else:
t = Task(dir,bsub,sufix,becld,zip)
self._list.append(t)
except:
return False
return True
def DoBackup(self):
for e in self._list:
try:
zip = zipfile.ZipFile(e.zip,'w',zipfile.ZIP_DEFLATED)
self.ZipDir(zip,e.dir,e.bsub,e.suffix,e.exclude)
zip.close()
except:
print("exception raised!")
return False
return True
def ZipDir(self,zip,dir,bsub,sfxs,ecld):
subdir = ""
path = ""
if os.path.isdir(dir):
paths = os.listdir(dir)
#备份本目录
print("ZipDir: ",dir)
for e in paths:
path = dir + "/" + e
ext = os.path.splitext(e)[1][1:]
if os.path.isfile(path) and Task.isfilter(ext,sfxs,ecld):
print ("ZipFile: ",path)
zip.write(path)
#清理子目录
if bsub:
for e in paths:
subdir = dir + "/" + e
self.ZipDir(zip,subdir,bsub,sfxs,ecld)
def PrintTask(self):
for e in self._list:
print (e.dir,e.bsub,e.suffix,e.exclude,e.zip)
if '__main__' == __name__:
c = QBackup()
c.ReadTask("bkup.txt")
c.DoBackup()
希望本文所述对大家Python程序设计的学习有所帮助。
标签:Python,备份,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
jsp自定义标签之ifelse与遍历自定义标签示例
2023-06-25 21:09:34
中文段首不需要空两格
2010-04-23 20:31:00
![](https://img.aspxhome.com/file/UploadPic/20104/23/text_non_spacep-19s.png)
sqlserver中求字符串中汉字的个数的sql语句
2012-06-06 19:53:37
[译稿]Web 字体的未来
2008-07-31 17:50:00
SQL Server中删除重复数据的几个方法
2009-10-30 10:50:00
php微信开发之批量生成带参数的二维码
2023-11-23 19:27:11
![](https://img.aspxhome.com/file/2023/8/113608_0s.jpg)
给zblog加上运行代码功能
2007-12-19 13:07:00
asp中文URL编码server.urlencode
2008-08-08 11:22:00
用ASP判断客户端浏览器语言自动跳转
2010-07-09 13:34:00
利用索引提高SQL Server数据处理的效率
2009-01-08 15:32:00
关于mysql与mysqli
2011-02-23 12:03:00
python3字符串操作总结
2023-08-23 22:31:23
php实现的微信分享到朋友圈并记录分享次数功能
2023-10-17 04:32:14
一个无组件上传的ASP代码
2007-10-09 19:49:00
JavaScript面向对象的简单介绍
2008-06-27 12:37:00
ASP中ServerVariables集合用法详解
2007-09-14 10:26:00
在IE6中用PNG图片实现半透明效果
2008-05-30 13:14:00
![](https://img.aspxhome.com/file/UploadPic/20085/30/2008530132733867s.jpg)
[译]2009年海外Web设计风潮(下)
2009-01-23 09:34:00
![](https://img.aspxhome.com/file/UploadPic/20091/23/trend-38-39s.gif)
简单的自定义php模板引擎
2023-11-24 02:44:24
asp中提示至少一个参数没有被指定值 解决方法
2013-06-01 20:14:00