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之装饰器、迭代器和生成器

在学习python的时候,三大“名器”对没有其他语言编程经验的人来说,应该算是一个小难点,本次博客就博主自己对装饰器、迭代器和生成器理解进行解释。 为什么要使用装饰器 什么是装饰器?“装...

python中的print()输出

1.普通的输出: print(str)#str是任意一个字符串,数字··· 2.格式化输出: print('1,2,%s,%d'%('asd',4)) 1,2,asd,4 与C语...

python实现的udp协议Server和Client代码实例

直接上代码:Server端:复制代码 代码如下: #!/usr/bin/env python # UDP Echo Server -  udpserver....

matplotlib实现热成像图colorbar和极坐标图的方法

matplotlib实现热成像图colorbar和极坐标图的方法

热成像图 %matplotlib inline from matplotlib import pyplot as plt import numpy as np def f(x,...

python openpyxl使用方法详解

openpyxl特点 openpyxl(可读写excel表)专门处理Excel2007及以上版本产生的xlsx文件,xls和xlsx之间转换容易 注意:如果文字编码是“gb2312”...