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设计】。

相关文章

基于pandas中expand的作用详解

expand表示是否把series类型转化为DataFrame类型 下面代码中的n表示去掉下划线"_"的数量 代码如下: import numpy as np import pand...

Python3随机漫步生成数据并绘制

Python3随机漫步生成数据并绘制

本文为大家分享了Python3随机漫步生成数据并绘制的具体代码,供大家参考,具体内容如下 random_walk.py from random import choice #生成随...

python 中的divmod数字处理函数浅析

divmod(a,b)函数 中文说明: divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数 返回结果类型为tuple 参数: a,b可以为数字(包括复数) 版本: 在...

python通过smpt发送邮件的方法

本文实例讲述了python通过smpt发送邮件的方法。分享给大家供大家参考。具体实现方法如下: import smtplib, socket fromaddr = a@b.com...

Python读取txt某几列绘图的方法

晚上帮同学用Python脚本绘图,大概需求是读取一个txt文件的两列分别作为x和y的值,绘图即可,代码如下: #coding:utf-8 import numpy as np imp...