简明python教程读书笔记(二)之为重要文件备份

一、可行性分析:

一般从经济、技术、社会、人四个方向分析。

二、需求分析:

需求分析就是需要实现哪些功能,这个很明了-文件备份

几个问题:

我们的备份位置?

什么时间备份?

备份哪些文件?

怎么样存储备份(文件类型)?

备份文件的名称?(需要通俗明了,一般是以当前时间命名)

三、实施过程:

方案一:

#!/usr/lib/env python

import os
import time
backlist=[‘/etc‘,‘/root‘]
to=‘/mnt/‘

target=to+time.strftime(‘%Y%m%d%H%M%S‘)+‘.tar.gz‘

gz_command="tar -czf %s %s"%(target,‘ ‘.join(backlist))

if os.system(gz_command)==0:
        print ‘successfull‘
else:
        print ‘failed‘

改进方案二:

方案二主要是考虑到多建日期子目录,这样更加明了
today = target_dir + time.strftime(‘%Y%m%d‘)
now = time.strftime(‘%H%M%S‘)

if not os.path.exists(today):
os.mkdir(today) # make directory
print ‘Successfully created directory‘, today

target = today + os.sep + now + ‘.zip‘
zip_command = "zip -qr ‘%s‘ %s" % (target, ‘ ‘.join(source))

注意:os.sep代表/或者\

改进方案三:可以给备份文件名称加注解:

#!/usr/bin/env python

import os

import time

source=[‘/var/log‘,‘/etc/ssh‘]

target=‘/mnt/‘

today=target+time.strftime(‘%Y%m%d‘)

now=time.strftime(‘%H%M%S‘)

comment=raw_input(‘pls input the comment-->‘)

if len(comment)==0:

target1=today+os.sep+now+‘.tar.gz‘

else:

target1=today+os.sep+now+‘_‘+comment.replace(‘ ‘,‘_‘)+‘.tar.gz‘

if not os.path.exists(today):

os.mkdir(today)

comm="tar -czf %s %s"%(target1,‘ ‘.join(source))

if os.system(comm)==0:

print ‘successfull‘

else:

print ‘failed‘

方案四:增加交互性

最理想的创建这些归档的方法是分别使用zipfile和tarfile模块。它们是Python标准库的一部分,可以

供你使用。使用这些库就避免了使用os.system这个不推荐使用的函数。

另外一方面可以通过交互式输入需要备份的目录然后用list的extend方法加到列表中去。

时间: 2024-08-07 06:55:54

简明python教程读书笔记(二)之为重要文件备份的相关文章

简明Python教程 读书笔记一

Python特性:解释性编程语言解释性——Python语言写的程序不需要编译成二进制代码.Python解释器把源代码转换成称为字节码的中间形式,然后再翻译成机器语言.面向对象——Python即支持面向过程的编程也支持面向对象的编程.在面向过程的语言中,程序是由过程或仅仅是可重用代码的函数构建起来的.在面向对象的语言中,程序是由数据和功能组合而成的对象构建起来的. 最初的步骤:有两种使用Python运行你的程序方式——使用交互式的带提示符的解释器或者使用源文件 退出python提示符——按Ctrl

[简明python教程]学习笔记之编写简单备份脚本

[[email protected] 0503]# cat backup_ver3.py #!/usr/bin/python #filename:backup_ver3.py import os import time #source source=['/root/a.sh','/root/b.sh','/root/c.sh'] #source='/root/c.sh' #backup dir target_dir='/tmp/' today=target_dir+time.strftime('

[简明python教程]学习笔记2014-05-05

今天学习了python的输入输出.异常处理和python标准库 1.文件 通过创建一个file类的对象去处理文件,方法有read.readline.write.close等 [[email protected] 0505]# cat using_file.py #!/usr/bin/python #filename:using_file.py poem='''Programing is fun when the work is done use Python! ''' f=file('poem.

《简明 Python 教程》笔记

<简明 Python 教程>笔记 原版:http://python.swaroopch.com/ 中译版:https://bop.mol.uno/ 有 int.float 没 long.double.没 char,string 不可变. help 函数 如果你有一行非常长的代码,你可以通过使用反斜杠将其拆分成多个物理行.这被称作显式行连接(Explicit Line Joining)5: s = 'This is a string. \ This continues the string.'

简明python教程学习笔记

参考(Reference) 不知道为什么没有翻译成引用. shoplist = ['apple', 'mango', 'carrot', 'banana']mylist = shoplist  这样写的话,mylist和shoplist指向同样的内存空间.那如果想要完全拷贝一份而不是引用shoplist应该怎么写呢? mylist = shoplist[:] 完整例子 切片(Slice) 昨天突然注意到切片的前后顺序.试了下,如果是[3:2]这样的范围的话,便输出一个空列表.当然[2:-1]这样

简明Python教程学习笔记1

1.介绍 略 2.安装Python 略 3.最初的步骤 (1)获取帮助help() help()的使用帮助 1 >>> help("help") 2 3 Welcome to Python 2.7! This is the online help utility. 4 5 If this is your first time using Python, you should definitely check out 6 the tutorial on the Inte

简明Python教程笔记(二)----用户交互raw_input()

raw_input() python内建函数 将所有输入看做字符串,返回字符串类型 input()对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float ) input() 本质上还是使用 raw_input() 来实现的,只是调用完 raw_input() 之后再调用 eval() 函数 例子: #!/usr/bin/env pythonthis_year = 2014name = raw_input('please input your name:')age1 =

简明Python教程笔记(一)

#!/usr/bin/env python#Filename : helloworld.py#The use of 'and"  print 'hello,world!'print "hello,world!" #The use of '''and"""print '''This is a multi-line string. This is the first line.This is the second line."What's

《简明Python教程》学习笔记

<简明Python教程>是网上比较好的一个Python入门级教程,尽管版本比较老旧,但是其中的基本讲解还是很有实力的. Ch2–安装Python:下载安装完成后,在系统的环境变量里,在Path变量后面追加安装目录的地址,即可在cmd下使用Python: CH3–Python3中,print的语法改为了print( ):Python编辑器列表:支持Python的IDE列表: CH4–变量不需要特别的变量类型定义过程: CH5–运算表达式及优先级: CH6–控制流,主控制语句行末以“:”结尾:if