Python教程中10.4 备份脚本代码中需注意的地方

开发 后端
在Python的实际应用中,你是否觉得Python要比其他的编程语言要好的多?但是在实际的应用中也有需要注意的地方,比如Python教程在10.4 备份脚本——版本四的代码。

你对Python编程语言的实际应用是否感兴趣?你是否了解其强大的实际运用功能?如果你对这些东西都特别的感兴趣的话,你就可以浏览以下的文章对其进行相关的了解,以下是文章的具体介绍。

Python教程

在例10.4 备份脚本——版本四的代码中有些需要注意的地方。

#!/usr/bin/python  
# Filename: backup_ver4.py  
import os  
import time  
# 1. The files and directories to be backed up are 
specified in a list.  
source = [‘/home/swaroop/byte’, ‘/home/swaroop/bin’]   # If you are using Windows, use source = 
[r'C:\Documents', r'D:\Work'] or something 
like that  
# 2. The backup must be stored in a main
 backup directory  
target_dir = ‘/mnt/e/backup/’ # Remember to 
change this to what you will be using  
# 3. The files are backed up into a zip file.   # 4. The current day is the name of the subdirectory
 in the main directory  
today = target_dir + time.strftime(‘%Y%m%d’)   # The current time 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 = raw_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’   # Notice the backslash!   # 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   # 5. We use the zip command (in Unix/Linux) to put 
the files in a zip archive  
zip_command = "zip -qr ‘%s’ %s" % (target, ‘ 
‘.join(source))  
# Run the backup   if os.system(zip_command) == 0:   print ‘Successful backup to’, target   else:   print ‘Backup FAILED’  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

关于Python教程的相关代码,文中提到“我还希望有的一个优化是使用tar命令替代zip命令。这样做的一个优势是在你结合使用tar和gzip命令的时候,备份会更快更小。如果你想要在Windows中使用这些归档,WinZip也能方便地处理这些.tar.gz文件。tar命令在大多数Linux/Unix系统中都是默认可用的。Windows用户也可以下载安装它。

命令字符串现在将成为:

tar = ‘tar -cvzf %s %s -X /home/swaroop/excludes.txt’
 % (target, ‘ ‘.join(srcdir)) 
  • 1.
  • 2.

需要补充的是Windows用户安装完LibArchive后命令为bsdtar,而且若想使用-z这个选项,还需安装gzip软件。

代码中的

target = today + os.sep + now + ‘.zip’ 
  • 1.

改为

target = today + os.sep + now + ‘.tar.gz’ 
  • 1.

使用

bsdtar -cvzf %s %s -X /home/swaroop/excludes.txt’ % (target, ‘ ‘.join(source)) 
  • 1.

 

英文原版已经没有用tar替换zip的内容了,提出建议用Python标准库中zipfile和tarfile来重写ver5。 以上就是对Python教程相关的内容的介绍,望你会有所收获。

【编辑推荐】

  1. Python源码分析的实际相关操作步骤解析
  2. Python安装的步骤操作其实是件很容易的事
  3. Python语言功能中的宏编程语言的实际操作方案介绍
  4. mod_python在性能上要优于传统CGI的缘由
  5. Python中文转换url编码的实际操作步骤介绍
责任编辑:佚名 来源: 博客园
相关推荐

2010-03-23 13:50:24

python教程

2013-04-07 11:31:05

云计算合同

2013-05-08 09:14:35

网络维护网吧网管

2010-09-25 17:57:48

项目管理

2010-10-08 15:58:22

IE JS

2010-07-26 15:30:43

面试

2011-05-05 14:27:17

喷码机耗材

2011-07-01 15:27:29

网站优化

2010-10-08 08:59:26

2010-05-31 10:55:08

Nagios mysq

2009-07-20 15:37:09

iBatis like注入漏洞

2011-07-03 23:40:54

友情链接

2015-04-16 16:42:11

私有云云部署

2011-03-31 16:24:16

cacti监控

2023-09-14 11:45:48

2017-07-31 09:53:31

2023-08-28 10:54:09

容器Docker

2011-06-24 09:34:38

外链SEO

2010-08-23 16:31:33

CSS

2013-01-15 10:22:16

点赞
收藏

51CTO技术栈公众号