在windows下用python脚本实现文件的备份的方法

这篇文章将为大家详细讲解有关在windows下用python脚本实现文件的备份的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

在windows下用python脚本实现文件的备份的方法:

#!/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')

关于“在windows下用python脚本实现文件的备份的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。