千家信息网

Python中10.4 备份脚本代码中需注意的地方有哪些

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章给大家介绍Python中10.4 备份脚本代码中需注意的地方有哪些,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。在例10.4 备份脚本——版本四的代码中有些需要注意的地方
千家信息网最后更新 2026年11月03日Python中10.4 备份脚本代码中需注意的地方有哪些

这篇文章给大家介绍Python中10.4 备份脚本代码中需注意的地方有哪些,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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

  1. #!/usr/bin/python

  2. # Filename: backup_ver4.py

  3. import os

  4. import time

  5. # 1. The files and directories to be backed up are
    specified in a list.

  6. source = [‘/home/swaroop/byte’, ‘/home/swaroop/bin’]

  7. # If you are using Windows, use source =
    [r'C:\Documents', r'D:\Work'] or something
    like that

  8. # 2. The backup must be stored in a main
    backup directory

  9. target_dir = ‘/mnt/e/backup/’ # Remember to
    change this to what you will be using

  10. # 3. The files are backed up into a zip file.

  11. # 4. The current day is the name of the subdirectory
    in the main directory

  12. today = target_dir + time.strftime(‘%Y%m%d’)

  13. # The current time is the name of the zip archive

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

  15. # Take a comment from the user to create the name
    of the zip file

  16. comment = raw_input(‘Enter a comment –> ‘)

  17. if len(comment) == 0: # check if a comment was entered

  18. target = today + os.sep + now + ‘.zip’

  19. else:

  20. target = today + os.sep + now + ‘_’ + \

  21. comment.replace(‘ ‘, ‘_’) + ‘.zip’

  22. # Notice the backslash!

  23. # Create the subdirectory if it isn’t already there

  24. if not os.path.exists(today):

  25. os.mkdir(today) # make directory

  26. print ‘Successfully created directory’, today

  27. # 5. We use the zip command (in Unix/Linux) to put
    the files in a zip archive

  28. zip_command = "zip -qr ‘%s’ %s" % (target, ‘
    ‘.join(source))

  29. # Run the backup

  30. if os.system(zip_command) == 0:

  31. print ‘Successful backup to’, target

  32. else:

  33. print ‘Backup FAILED’

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

命令字符串现在将成为:

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

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

代码中的

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

改为

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

使用

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

英文原版已经没有用tar替换zip的内容了,提出建议用Python标准库中zipfile和tarfile来重写ver5。

关于Python中10.4 备份脚本代码中需注意的地方有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

命令 代码 备份 地方 脚本 内容 更多 用户 帮助 不错 优势 兴趣 原版 字符 字符串 小伙 小伙伴 建议 教程 文件 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技图片 广东猪兼强互联网科技有限公司 云之行互联网科技 科技与互联网 互联网科技的 唐信互联网科技 互联网科技小额贷款 狮子座互联网科技培训 互联网科技的未来发展 无锡溥仪互联网科技有限公司 互联网之后的科技是什么 呼和浩特新华互联网科技学院 中国互联网十大科技股排名 爱医互联网络科技 中和天地互联网科技有限公司 互联网第三次科技革命特征 南通创西澳互联网科技有限公司 上海创道互联网络科技有限公司 互联网科技包装股份有限公司上市 科技互联网专业务虚会材料 北京路客互联网科技有限公司 互联网巨头科技公司 汇久创客互联网科技 时间互联网络科技 互联网科技股有哪些 无锡溥仪互联网科技有限公司 青岛科技大学互联网大赛研究生 全球科技与互联网公司市值变化 伍佰万互联网科技是干嘛的 互联网 科技节活动 微视频大赛
0