Python在window下备份脚本

很多关于python在linux下的例子,如果不是对python有充分的理解,你就不会运用python在window下运用。根据python在linux的例子改为window,如下:

#!/usr/bin/python 
# Filename: backup_ver1.py 

import os 
import time 

# 1. The files and directories to be backed up are specified in a list. 
source = [r'C:\python', r'C:\python'] 

# 2. The backup must be stored in a main backup directory 
target_dir = 'python' # Remember to change this to what you will be using 

# 3. The files are backed up into a zip file. 
# 4. The name of the zip archive is the current date and time 
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip' 

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive 
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source)) 


# Run the backup 
if os.system(zip_command) == 0: 
  print 'Successful backup to', target 
else: 
  print 'Backup FAILED'





这是运行的结果。

但是你会发现生成的文件最终为'python20120115122735.zip',所以去掉单引号window下就可以识别了。

这个python脚本功能是备份window下C盘的一个python文件夹。当你用脚本在哪里运行,就会备份到哪里的,如你在D盘运行该脚本,就会在D盘下生成备份文件

(source = [r'C:\python', r'D:\dkj'] 这相当于一个列表,同时备份C盘的python文件夹和D盘的dkj文件夹)

你可能感兴趣的:(Python在window下备份脚本)