• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python实现备份文件实例

Python实现备份文件实例

作者:shichen2014 字体:[增加 减小] 来源:互联网

shichen2014 通过本文主要向大家介绍了python 备份文件,python备份,python备份脚本,python脚本编写实例,python项目实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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 ; 备份后的文件路径名

</div>

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()
 
</div>

希望本文所述对大家Python程序设计的学习有所帮助。

</div>

您可能想查找下面的文章:

  • Python 备份程序代码实现
  • Python备份目录及目录下的全部内容的实现方法
  • Python实现新浪博客备份的方法
  • Python实现新浪博客备份的方法
  • Python实现网站文件的全备份和差异备份
  • Python实现备份文件实例
  • python备份文件以及mysql数据库的脚本代码
  • python备份文件以及mysql数据库的脚本代码
  • Python备份Mysql脚本
  • python备份文件的脚本

相关文章

  • python实现问号表达式(?)的方法
  • Python正则表达式介绍
  • Python中返回字典键的值的values()方法使用
  • Python lxml模块安装教程
  • Python使用代理抓取网站图片(多线程)
  • Python脚本实时处理log文件的方法
  • Python多进程同步Lock、Semaphore、Event实例
  • Python实现控制台进度条功能
  • 跟老齐学Python之集合的关系
  • 多线程爬虫批量下载pcgame图片url 保存为xml的实现代码

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python实现颜色空间转换程序(Tkinter)
    • Python常用小技巧总结
    • python中子类继承父类的__init__方法实例
    • Python入门教程之运算符与控制流
    • Python实现冒泡,插入,选择排序简单实例
    • 详解python中requirements.txt的一切
    • pyqt4教程之messagebox使用示例分享
    • 在Django的视图(View)外使用Session的方法
    • 讲解Python中的标识运算符
    • 一个基于flask的web应用诞生 记录用户账户登录状态(6)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有