首页 > 编程语言 > 详细

Python 备份文件 windows

时间:2014-09-12 18:57:53      阅读:309      评论:0      收藏:0      [点我收藏+]

学习Python时遇到一个备份的问题, 下面记录下。

使用Python 在windows 下备份文件。

有几个需要注意的地方:

1.  需下载 7 zip 下载地址 :   http://www.7-zip.org/download.html  根据自己的系统下载对应的版本

2.  7 zip 安装路径中的文件夹名不能出现空格, 以我的为例默认是安装在 C:\Program Files 下。  

      Program Files 文件夹中有空格会导致导入失败,安装的时候自己按需要替换下. (我的安装路径 :C:\7-Zip)

 

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

import os
import time

# 1. The files and directories to be backed up are specified in a list.
source = [r‘C:\Python34\Doc‘]
# If you are using Windows, use source = [r‘C:\Documents‘, r‘D:\Work‘] or something like that

# 2. The backup must be stored in a main backup directory
target_dir = r‘D:\Myback\\‘ # 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 = r"C:\7-Zip\7z.exe a %s %s" % (target, ‘‘.join(source)) 

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

  

Python 备份文件 windows

原文:http://www.cnblogs.com/minco/p/3968853.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!