python2.7实现备份

来源:互联网 发布:js this用法 编辑:程序博客网 时间:2024/06/03 04:16

刚刚修改了下简明python教程的第一个备份例子

鉴于我是在windows下操作的,修改如下

import os

import time
yuanwenjian=[r'e:\beifentest',r'e:\beifentest1']
mubiaomulu='e:\\pythonbeifentest\\'

mubiaolujing=mubiaomulu+'w'+time.strftime('%Y%m%d%H%M%S')+'.rar'

#下面这句是最重要的,winrar命令行可以百度下,注意winrar命令行的环境变量,当然你可以设置下,否则提示找不到命令,注意地方就是winrar命令行参数中目录是用双引号括住的。

mingling="winrar a \"%s\" %s"%(mubiaolujing,' '.join(yuanwenjian))
if os.system(mingling)==0:
   print 'successful backup to',mubiaolujing
else:

   print 'failed'

成功~

不能自动创建目录

我把

mubiaolujing=mubiaomulu+'w'+time.strftime('%Y%m%d%H%M%S')+'.rar'

删除后

有得搞

基于简明python教程,一步一步模仿下来

第二次修改:(主要是对备份时间的管理)

import os
import time
import os.path
yuan=["e:\\beifentest","e:\\beifentest1"]
jintian="e:\\"+time.strftime('%Y%m%d')
shijian=time.strftime('%H%M%S')
if os.path.exists(jintian):
   print "get it"
else:  
   os.mkdir(jintian)
   print "successful to creat"

mubiao=jintian+os.sep+shijian+".zip"
mingling="winrar a \"%s\" %s"%(mubiao,' '.join(yuan))
if os.system(mingling)==0:
   print "successful"
else:
   print "failed"

注:os.sep其实就是自动适配分隔符,window就是\\,它们说为了可移植性,写的过程又忘了目录的表示,\\这样的

第三次修改:(主要是:备份了什么玩意,HUAWEI.zip,APPLE.zip)

起初思路:我们备份的时候会提示备份到HUAWEI这个分类,还是APPLE这个分类。确认并回车后,即可分类的压缩

简明python思路就是:跟我 差不多,只是获取用户键入的信息,多了个处理空格的操作,这是因为处理这样的文件名要容易得多,没必要了,win7支持空格!

import os
import time
import os.path
yuan=["e:\\beifentest","e:\\beifentest1"]
jintian="e:\\"+time.strftime('%Y%m%d')
shijian=time.strftime('%H%M%S')
tip=raw_input("input backup tips:")
if os.path.exists(jintian):
   print "get it"
else:  
   os.mkdir(jintian)
   print "successful to creat"
mubiao=jintian+os.sep+shijian+tip+".zip"
mingling="winrar a \"%s\" %s"%(mubiao,' '.join(yuan))
if os.system(mingling)==0:
   print "successful"
else:
   print "failed"
  


学习到:内建函数raw_input(),字符串方法

str.replace()

str.replace(old,new[,count])

Return a copy of the string with all occurrences of substring old replaced bynew. If the optional argumentcount is given, only the first count occurrences are replaced.。

             返回这字符串(也就是str)的副本,这副本中有 old的字眼都会被new取代掉。如果可选项count赋值了,那么对应的替代(看我下图吧)


raw_input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read,EOFError is raised. Example:

如果prompt变量出现了,它被写到标准输出(也就是会被用户看到)不换行。这功能之后会读取来自输入的一行,转化它成一个字符串(拆除一个尾随的换行),并且返回它(字符串)。当EOF被读取(也就是用户没输入什么花露),EOFError会被引发。例如:

>>> s = raw_input('--> ')--> Monty Python's Flying Circus>>> s"Monty Python's Flying Circus"

If the readline module was loaded, thenraw_input() will use it to provide elaborate line editing and history features.

             如果readline模块被加载,raw-input会使用它去提供一个详细的行编辑和历史特性

第四次修改:进一步优化

简明教程思路:备份的目录自定义(用户键入目录),通过sys.argv列表来获取它们,然后我们可以使用list类提供的extend方法把它们加到source列表中去。至于打包命令的优化,我这里依旧使用winrar吧。最理想的创建这些归档的方法是分别使用zipfiletarfile。它们是Python标准库的一部分,可以供你使用。使用这些库就避免了使用os.system这个不推荐使用的函数,它容易引发严重的错误。然而,我在本节中使用os.system的方法来创建备份,这纯粹是为了教学的需要。这样的话,例子就可以简单到让每个人都能够理解,同时也已经足够用了




0 0