设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 5502|回复: 0
打印 上一主题 下一主题

向老师请教一个打包文件的问题

[复制链接]

6

主题

0

好友

166

积分

注册会员

Rank: 2

跳转到指定楼层
楼主
发表于 2019-1-19 17:37:34 |只看该作者 |倒序浏览
  1. import os
  2. import time

  3. # 1. 需要备份的文件与目录将被
  4. # 指定在一个列表中。
  5. source = [r'C:\Users\Dong\Desktop\August']

  6. # 2. 备份文件必须存储在一个
  7. # 主备份目录中
  8. target_dir = r'C:\Users\Dong\Desktop\August2'
  9. # 要记得将这里的目录地址修改至你将使用的路径

  10. # 如果目标目录还不存在,则进行创建
  11. if not os.path.exists(target_dir):
  12.     os.mkdir(target_dir) # 创建目录

  13. # 3. 备份文件将打包压缩成zip文件。
  14. # 4. 将当前日期作为主备份目录下的
  15. # 子目录名称
  16. today = target_dir + os.sep + time.strftime('%Y%m%d')
  17. # 将当前时间作为 zip 文件的文件名
  18. now = time.strftime('%H%M%S')

  19. # 添加一条来自用户的注释以创建
  20. # zip 文件的文件名
  21. comment = input('Enter a comment --> ')
  22. # 检查是否有评论键入
  23. if len(comment) == 0:
  24.     target = today + os.sep + now + '.zip'
  25. else:
  26.     target = today + os.sep + now + '_'+ comment.replace(' ', '_') + '_' + '.zip'

  27. # 如果目标目录不存在则创建目录,Windows下不要这部分,因为能自动创建且重复创建会报错
  28. #if not os.path.exists(today):
  29.     #os.mkdir(today)
  30.     #print('Successfully created directory', today)

  31. # 5. 我们使用 zip 命令将文件打包成zip格式
  32. zip_command = 'zip - r {0} {1}'.format(target, ''.join(source))
  33. #zip_command = 'zip - r {0} {1}'.format(target,source[0])

  34. # 运行备份
  35. print('zip_command is:')
  36. print(zip_command)
  37. print('Running:')
  38. if os.system(zip_command) == 0:
  39.     print('successful backup to', target)
  40. else:
  41.     print('bacup failed')
  42. print('Done')
复制代码
Enter a comment --> 5 6 5 7
zip_command is:
zip - r C:\Users\Dong\Desktop\August2\20190119\173538_5_6_5_7__.zip C:\Users\Dong\Desktop\August
Running:
    zip warning: name not matched: r
    zip warning: name not matched: C:\Users\Dong\Desktop\August2\20190119\173538_5_6_5_7__.zip

zip error: Interrupted (aborting)
bacup failed
Done


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-4-20 08:59 , Processed in 0.016203 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部