文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>python_实例: 文件备份程序

python_实例: 文件备份程序

时间:2011-05-29  来源:邵国宝

问题描述:

我提出的问题是: 我想要一个可以为我的所有重要文件创建备份的程序。

版本一:

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

import os
import time

#1. The files and directories to be backuped are specificed in a list
source = ['/root/test_backup/test_file', '/root/test_backup_dir/']

#2. The backup must be stored in a main backup directory
target_dir = '/root/target_dir/'

#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 file
zip_command = "zip -qr '%s' %s"%(target, ' '.join(source))

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

[注] 这个比较简单。

版本二:

我认为优化之一是采用更好的文件名机制——使用 时间 作为文件名,而当前的 日期 作为目录名,存放在主备份目录中。这样做的一个优势是你的备份会以等级结构存储,因此它就更加容易管理了。另外一个优势是文件名的长度也可以变短。还有一 个优势是采用各自独立的文件夹可以帮助你方便地检验你是否在每一天创建了备份,因为只有在你创建了备份,才会出现那天的目录。

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

import os
import time

source
= ['/root/test_backup/test_file', '/root/test_backup_dir']

target_dir
= '/root/target_dir/' + time.strftime('%Y%m%d') + '/'

target
= target_dir + time.strftime('%H%M%S') + '.zip'

zip_command
= "zip -qr '%s' %s" % (target, ' '.join(source))
mkdir_command
= 'mkdir -p ' + target_dir

if os.system(mkdir_command) != 0:
print 'mkdir FAILED'

if os.system(zip_command) == 0 :
print "Successful backup to", target
else :
print "Backup FAILED"

[注] 自己写的,感觉不如书上写的好,下面这个是书上写的

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

import os
import time

source
= ['/root/test_backup/test_file', '/root/test_backup_dir']
target_dir
= '/root/target_dir/'

today
= target_dir + time.strftime('%Y%m%d')
now
= time.strftime('%H%M%S')

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

target
= today + os.sep + now + '.zip'

zip_command
= "zip -qr '%s' %s" % (target, ' '.join(source))

if os.system(zip_command) == 0 :
print 'Sucessful backup to', target
else:
print 'Backup FAILED'

[注] 不是写的比我好,是写的比我的好多了。

注意os.sep变量的用法——这会根据你的操作系统给出目录分隔符,即在Linux、Unix下它是'/',在Windows下它是'\\',而在Mac OS下它是':'。使用os.sep而非直接使用字符,会使我们的程序具有移植性,可以在上述这些系统下工作。

我写的程序考虑的不周全,移植是一个,而且没有把备份的主目录给独立出来。

版本三:

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

import os
import time

source
= ['/root/test_backup/test_file', '/root/test_backup_dir']
target_dir
= '/root/target_dir/'

today
= target_dir + time.strftime('%Y%m%d')
now
= time.strftime('%H%M%S')

#Take a comment from the user to create the name of the zip file
comment = raw_input('Enter a comment -->')

if len(comment) == 0 :
target
= today + os.sep + now + '.zip'
else :
target
= today + os.sep + now + '_' + \
comment.replace(
' ', '_') + '.zip'
#Notice the backslash

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

zip_command
= "zip -qr '%s' %s" % (target, ' '.join(source))

if os.system(zip_command) == 0 :
print 'Successful backup to', target
else :
print 'Backup FAILED'

程序写的越来越像会事了。

相关阅读 更多 +
排行榜 更多 +
耶小兔子2

耶小兔子2

休闲益智 下载
nba2k20豪华版

nba2k20豪华版

体育竞技 下载
画线征服火柴人安卓版

画线征服火柴人安卓版

动作格斗 下载