【python】python解决问题

1、需求:我想要一个给我所有的重要文件备份的程序

2、需求分析:我们如何确定该备份哪些文件?备份保存在哪里?我们怎样存储备份?

3、程序设计:

列一个程序如何工作的顺序表:

(1)需要备份的文件和目录由一个列表指定。

(2)备份应该保存在主备份目录中

(3)文件备份成一个.zip文件。

(4)zip存档的名称是当前的日期和时间。

(5)我们使用标准的zip命令。可以选择其他命令,但是必须有命令行界面。这样我们才可以从脚本中传递参数。

4、解决方案:

------

#C:/Python33/HwhCode
#Filename: backup_verl.py
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = ['"C:\\Code1"','C:\\Code']
# Notice we had to use double quotes inside the string for names with spaces in it
# 2. The backup must be stored in a main backup directory
target_dir = 'C:\\Backup' #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 + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
# 5. We use the zip command to put the files in a zip arcgive
zip_command = "zip -qr {0} {1}".format(target,' '.join(source))
#zip_command = "rar a {0} {1}".format(target,' '.join(source))
#Run the backup
if os.system(zip_command) == 0:
    print('Succeddful bakcup to',target)
else:
    print('Backup FAILED')
------


5、测试
(1)以上程序没有成功,在os.system前设置一条语句print(zip_command)。运行程序。


复制打印出来的zip_command命令到shell提示符,看看能否正常运行。


命令失败,查看zip命令手册,到底是什么出错了。(如果命令正常,查看python程序是不是写错了。)
在这里是因为我的电脑不支持zip的命令,但是我安装了WINRAR,所以可以使用rar命令代替zip命令。
将上面的zip_command = 'zip r {0} {1}'.format(target,' '.join(source))注释掉。换成zip_command = 'rar a {0} {1}'.format(target,' '.join(source))
运行。
依然失败。
打开电脑自带的cmd窗口,打入rar直接运行,找不到。这就是因为rar的环境变量没有设置。
在windows环境变量的PATH中加入rar.exe所在的目录地址。重新打开cmd,运行rar就可以找到了。
关闭python,重新打开,再运行修改后的程序,成功了。


知识点解析:
(1)我们使用了os和time模块,所以import了它们。
(2)我们在source列表中指定了要备份的文件目录,目标目录是我们想要存储备份文件的地方,由target_dir指定。zip归档的名称是目前的日期和时间。使用time.strftime函数获得。它还包括了.zip扩展名,被保存在target_dir目录中。
(3)os.sep变量,(需要根据操作系统给出路径分隔符,Linux下用/,windows下用\\,Mac中用:),使用os.sep而不是直接使用这些符号,会使程序更加简洁,并且能在这些系统下正常运行。
(4)time.strftime()函数需要我们在上面的程序中使用的那种定制。%Y会被无世纪的年份代替。%m会被01到12之间的一个十进制的月份数代替。
(5)我们用添加操作符(将多个字符串连接到一起,例如,两个字符串连接到一起,返回一个新的字符串)创建了名为target.zip的文件,然后创建了一个zip_command字符串。这个字符串包含了将要执行的命令。可以通过在shell运行来检查命令的正确性。
(6)使用zip命令有一些选项和传递的参数。-q选项被用来表示zip命令应该以快速的方式进行。-r表示指定zip命令应该对每个目录重复执行,例如应该包含所有子目录和文件。这两个选项可以用-qr结合起来。选项后面跟的是要备份到zip文档的文件和目录列表。我们用join方法将资源列表转换成一个字符串。
(7)我们最后用os.system函数运行命令,该函数就好像我们在系统中直接运行命令一样。命令运行成功返回0,否则返回错误代码。根据命令的返回结果,我们打印备份是否成功。
6、软件维护环节:
(1)优化之一是更好的文件名机制,使用时间作为文件名,而当前的日期作为目录名,存放在主备份目录中。这样备份的优势是,备份会以等级结构存储,更容易管理,文件名的长度也可以变短。另外一个优势是,使用各自独立的文件夹,可以帮助你方便的检验是否在每一天都备份了。因为只有你创建了备份,才会出现那一天的目录。
--------

#C:/Python33/HwhCode
#Filename: backup_verl_update.py
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = [r'"C:\Code 1"',r'C:\Code']
#source = r'C:\Code1'
# Notice we had to use double quotes inside the string for names with spaces in it
# 2. The backup must be stored in a main backup directory
target_dir = r'C:\Backup' #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
today = target_dir +os.sep +time.strftime('%Y%m%d')
# The current day is the name of the zip archive
now = time.strftime('%H%M%S')
#Create the subdirectory if it isn't already there
if not os.path.exists(today):
    os.mkdir(today)#make directory
    print('Successfully created directory',today)
#The name of the zip file
target = today +os.sep +now+'.zip'
# 5. We use the zip command to put the files in a zip arcgive
#zip_command = 'zip r {0} {1}'.format(target,' '.join(source))
zip_command = 'rar a {0} {1}'.format(target,' '.join(source))
#print(zip_command)
#Run the backup
if os.system(zip_command) == 0:
    print('Succeedful bakcup to',target)
else:
    print('Backup FAILED')
------


(1)os.exists函数检验在主备份目录中是否有以当前日期作为名称的目录。如果没有,我们使用os.mkdir函数创建。
(2)os.sep变量的用法,会根据操作系统给出目录分隔符,即,在linux下它是/,在windows下它是\\。在mac下是:。使用os.sep而非直接使用字符,会使我们的程序具有移植性,可以在上述这些系统下工作。
7、软件升级环节
第二个版本在做较多备份的时候还工作的不错,但是如果有极多备份的时候,我发现要区分每个备份是干什么的,会很困难。
例如对程序做了一些重要的改变,于是我想要把这些改变与zip归档的名称联系起来。这可以通过在zip归档名上附带一个用户提供的注释来方便的实现。
-------

#C:/Python33/HwhCode
#Filename: backup_verl3_update.py
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = [r'"C:\Code 1"',r'C:\Code']
# source = r'C:\Code1'
# Notice we had to use double quotes inside the string for names with spaces in it
# 2. The backup must be stored in a main backup directory
target_dir = r'C:\Backup' #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
today = target_dir +os.sep +time.strftime('%Y%m%d')
# The current day is the name of the zip archive
now = time.strftime('%H%M%S')
# Take a comment from the user to create the name of the zip file
comment = input('Enter a comment --> ')
if len(comment) == 0: #check if a comment was entered
    target = today +os.sep +now + '.zip'
else:
    target = today +os.sep+now+ '_' + comment.replace(' ','_')+'.zip'

#Create the subdirectory if it isn't already there
if not os.path.exists(today):
    os.mkdir(today)#make directory
    print('Successfully created directory',today)
    
#The name of the zip file
#target = today +os.sep +now+'.zip'
    
# 5. We use the zip command to put the files in a zip arcgive
#zip_command = 'zip r {0} {1}'.format(target,' '.join(source))
zip_command = 'rar a {0} {1}'.format(target,' '.join(source))
#print(zip_command)
#Run the backup
if os.system(zip_command) == 0:
    print('Succeedful bakcup to',target)
else:
    print('Backup FAILED')
-------


8、提炼:

(1)可以在程序中包含交互程度————使用-v选项来使程序更具交互性。

(2)可以使文件和目录能够通过命令行直接传递给脚本。可以用sys.argv列表来获取它们,然后我们可以使用list类提供的extend方法把他们加到source列表中去。

(3)最重要的提炼就是不用os.system方法来创建压缩文档,二是用zipfile或者tarfile内置模块来创建压缩文档。它们是标准库的一部分,没有与你计算机中外部zip程序的依赖性,并且已经能够使用。

9、软件开发过程:

(1)什么(分析)

(2)如何(设计)

(3)编写(实施)

(4)测试(测试与调试)

(5)使用(实施或开发)

(6)维护(优化)


  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值