python实现windows下文件备份脚本

yipeiwu_com6年前Python基础

在windows下用python脚本实现文件的备份,参考《A Byte of Python3》第十一章(Page59)。

#!/usr/bin/python 
# -*- coding: cp936 -*- 
 
import os 
import time 
 
source = ['E:\\'] # 待备份文件在E盘根目录下 
running = True 
 
while running: 
  your_source = raw_input("Your own path or your own file path:") 
  #如果使用input(),在运行后输入路径名时,需要在两边加上" ",下面的input同理 
  #比如欲备份E盘下zipme文件夹里的hello.txt文件,则应输入zipme\\hello.txt 
  source.append(your_source) 
  if raw_input("Do you want to add file or folder(y/n):")=='n': 
    running = False 
 
target_dir = 'E:\\backup\\' #备份生成的文件存放的路径 
 
#以当前日期和时间为文件名命名生成的压缩文件 
target = target_dir+\ 
     time.strftime('%Y')+\ 
     time.strftime('%m')+\ 
     time.strftime('%d')+\ 
     time.strftime('%H')+\ 
     time.strftime('%M')+\ 
     time.strftime('%S')+'.rar' 
 
#使用zip命令压缩文件 
zip_command = "zip -qr {0} {1}".format(target, ''.join(source)) 
 
#通过给系统传递参数来执行压缩命令(压缩使用的是WinRAR所带文件rar.exe来执行压缩) 
if os.system(zip_command) == 0: 
  print('Successful backup to',target) 
else: 
  print('Backup FAILED') 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现Logger打印功能的方法详解

前言 众所周知在Python中有专门用于logger打印的套件叫logging,但是该套件logger仅接收一个字符串类型的logger打印信息。因此,我们在使用是需要先提前将要打印的信...

儿童学习python的一些小技巧

以下是一些Python实用技巧和工具,希望能对大家有所帮助。 交换变量 x = 6 y = 5 x, y = y, x print x >>> 5 print...

Python装饰器使用示例及实际应用例子

Python装饰器使用示例及实际应用例子

测试1 deco运行,但myfunc并没有运行 复制代码 代码如下: def deco(func):     print 'before func' &nb...

python学习数据结构实例代码

在学习python的过程中,用来练习代码,并且复习数据结构的 #coding:utf-8 #author:Elvis class Stack(object): def _...

将matplotlib绘图嵌入pyqt的方法示例

我的终极整理,供参考 # coding:utf-8 import matplotlib # 使用 matplotlib中的FigureCanvas (在使用 Qt5 Backends...