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 = ...
随机推荐
- hdu 3863 No Gambling
#include<stdio.h> int main() { int n; ) { printf("I bet on Oregon Maple~\n"); } ; } ...
- android MD5
public static String MD5(String str) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstan ...
- *windows文件显示后缀名
- PCL—低层次视觉—点云滤波(基于点云频率)
1.点云的频率 今天在阅读分割有关的文献时,惊喜的发现,点云和图像一样,有可能也存在频率的概念.但这个概念并未在文献中出现也未被使用,谨在本博文中滥用一下“高频”一词.点云表达的是三维空间中的一种信息 ...
- linux查看硬件环境
一:查看cpu more /proc/cpuinfo | grep "model name" grep "model name" /proc/cpuinfo 如 ...
- openfire的smack和asmack
smack你可以看成是一套封装好了的用于实现XMPP协议传输的API,它是一个非常简单并且功能强大的类库,给用户发送消息只需要三行代码.下载地址:http://www.igniterealtime.o ...
- cocos2dx 的基本框架
AppDelegate.h #ifndef _APP_DELEGATE_H_ #define _APP_DELEGATE_H_ #include "cocos2d.h" USING ...
- centos防火墙设置
1.查看 service iptables status 2.开关 service iptables start/stop 3.开机启动 chkconfig iptables on/off 4.编辑端 ...
- linux 命令学习大全
转载 http://blog.csdn.net/xiaoguaihai/article/details/8705992/
- Regex Failure - Bug Fixing #2
http://www.codewars.com/kata/55c423ecf847fbcba100002b/train/csharp Oh no, Timmy's received some hate ...