码迷,mamicode.com
首页 > 编程语言 > 详细

Python备份脚本(Win10+Python2.7+PyCharm)

时间:2015-09-06 22:59:49      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:

说一下程序来源,是从《Python简明教程》上面看到的程序,试了一下之后,居!然!不!行!!!

Google了老半天,也看了好多个博客,也未能解决。

除了一些基本语法问题、字符串中队‘\‘的处理的问题等之外,此处假设程序本身没什么问题了,主要是zip_command这个指令出现的问题。其中最常见的问题就是zip不是什么内部指令的那个了,如下图:

 

 

技术分享

先说一下我的解决方法,是从万能的知乎上找到的方案,话不多说,直奔重点。

下了一个7-zip的软件,安装一下,要记住你的安装路径!

先贴代码吧,如下所示:

 1 # -*- coding:utf-8 -*-
 2 
 3 __author__ = Leon
 4 
 5 import os
 6 import time
 7 
 8 #1 先把需要备份的文件夹,保存进列表
 9 source = [rG:\test,rG:\test2]
10 #2 设置目标保存文件目录
11 target_dir = D:\\
12 #3 保存为zip格式,规范命名
13 target = target_dir + time.strftime(%Y%m%d%H%M%S) + .zip
14 
15 print source
16 print target
17 
18 #4 使用zip command命令,将文件压缩进zip存档
19 zip_command = r"D:\7-Zip\7z.exe a %s %s" % (target, .join(source))#注意,这里的‘‘之间有一个单位的空格符
20 
21 #运行
22 if os.system(zip_command) == 0:
23     print 文件已经成功备份至 + target
24 else:
25     print 备份失败...

将zip_command中的的zip命令定向性的指明位置,D:\7-zip\7z.exe就是软件的安装位置。

下面是我的输出结果:

D:\Python27\python.exe E:/MyDamnWork/backup_ver01/ver_01.py
[‘G:\\test‘, ‘G:\\test2‘]
D:\20150906214804.zip

7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Scanning

Creating archive D:\20150906214804.zip

Compressing test2\b (1).docx
Compressing test2\b (1).pdf
Compressing test2\b (1).txt
Compressing test2\b (2).docx
Compressing test2\b (2).txt
Compressing test\a (1).jpg
Compressing test\a (2).jpg
Compressing test\a (3).jpg
Compressing test\a (4).jpg
Compressing test\a (5).jpg

Everything is Ok
文件已经成功备份至D:\20150906214804.zip

Process finished with exit code 0

 

打开压缩文件后:

技术分享

 原文链接:http://www.cnblogs.com/leonwen/p/4787492.html

Python备份脚本(Win10+Python2.7+PyCharm)

标签:

原文地址:http://www.cnblogs.com/leonwen/p/4787492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!