Python 实例: 备份文件
都说生命苦短,我用python, 所以这两天我也开始学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写小应用的确很爽啊!C++就太笨重了!
Python 实例: 备份文件的更多相关文章
- 第一个python实例--监控cpu
#第一个python实例:监控cpu #/bin/bash/env Python from __future__ import print_function from collections impo ...
- (转)Python实例手册
原文地址:http://hi.baidu.com/quanzhou722/item/cf4471f8e23d3149932af2a7 实在是太好的资料了,不得不转 python实例手册 #encodi ...
- 使用docker安装部署Spark集群来训练CNN(含Python实例)
使用docker安装部署Spark集群来训练CNN(含Python实例) http://blog.csdn.net/cyh_24/article/details/49683221 实验室有4台神服务器 ...
- 【NLP】Python实例:申报项目查重系统设计与实现
Python实例:申报项目查重系统设计与实现 作者:白宁超 2017年5月18日17:51:37 摘要:关于查重系统很多人并不陌生,无论本科还是硕博毕业都不可避免涉及论文查重问题,这也对学术不正之风起 ...
- 转载 python实例手册
python实例手册 #encoding:utf8# 设定编码-支持中文 0说明 手册制作: 雪松 更新日期: 2013-12-19 欢迎系统运维加入Q群: 198173206 # 加群请回答问题 请 ...
- 【NLP】Python实例:基于文本相似度对申报项目进行查重设计
Python实例:申报项目查重系统设计与实现 作者:白宁超 2017年5月18日17:51:37 摘要:关于查重系统很多人并不陌生,无论本科还是硕博毕业都不可避免涉及论文查重问题,这也对学术不正之风起 ...
- python实例、类方法、静态方法
[python实例.类方法.静态方法] 参考:http://blog.163.com/yang_jianli/blog/static/161990006201122411586729/
- 【转载】python实例手册
今天写爬虫的时候遇到了问题,在网上不停地查找资料,居然碰到两篇好文章: 1.python实例手册 作者:没头脑的土豆 另一篇在这:shell实例手册 python实例手册 #encoding:ut ...
- Python实例---利用正则实现计算器[FTL版]
import re # 格式化 def format_str(str): str = str.replace('--', '+') str = str.replace('-+', '-') str = ...
随机推荐
- 关于Tomcat 6的热部署和热加载
转载:http://blog.csdn.net/tianlincao/article/details/7263840 之前每当修改了类文件,保存后tomcat 都需要自动重启,今天作了后面的设置后,可 ...
- HTML5 编辑 API 之 Range 对象(二)
1.Range.cloneContents()The Range.cloneContents() returns a DocumentFragment copying the objects of t ...
- Lambda 表达式型的排序法
int[] arry = {3,9,5,7,64,51,35,94 }; foreach (int i in arry.OrderBy(i => i)) Console.WriteLine(i) ...
- iOS 精确定时器
Do I need a high precision timer? Don't use a high precision timer unless you really need it. They c ...
- 261. Graph Valid Tree
题目: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nod ...
- 深度神经网络入门教程Deep Neural Networks: A Getting Started Tutorial
Deep Neural Networks are the more computationally powerful cousins to regular neural networks. Learn ...
- [Mongo] error inserting documents: BSONObj size is invalid (mongoimport mongorestore 数据备份恢复)
解决办法如下, ./mongoimport -port 6066 -d xxx -c xxx --batchSize=10 /root/mong_data/test/xxx 原因转自 http://b ...
- 写给系统管理员的25个PHP安全实践
PHP是广泛使用的开源服务端脚本语言.通过HTTP或HTTPS协议,Apache Web服务允许用户访问文件或内容.服务端脚本语言的错误配置会导致各种问题.因此,PHP应该小心使用.以下是为系统管理员 ...
- GIT使用教程与基本原理
转自:http://blog.csdn.net/wengpingbo/article/details/8985132 说明:该教程全部图片都来自于<pro git>.以下所有的操作,除非特 ...
- svn: E230001: Server SSL certificate verification failed
TortoiseSvn是好的 命令行svn 的时候 有问题 ,也加了--no-auth-cache --non-interactive参数 svn list 地址 选下p 就好. http://sta ...