Python3-demo-备份文件并打包 zip

备份文件并打包成 zip

  • 需要备份的文件目录,打包后的目录
  • 打包 zip
  • zip 文件名由时间命名
    示例代码
import os
import time

# 1.需要备份的文件路径
source = [' /Users/let/Desktop/UI图/File']

# 2.备份文件存储位置路径
targetSource = '/Users/let/Desktop/saveFile'

# 3.将备份文件打包压缩 zip 文件,然后以当前时间命名 zip 文件名
target = targetSource + os.sep + \
         time.strftime('%Y%m%d%H%M%S') + '.zip'

# 4.使用 zip 命令将文件打包成 zip 格式
zip_command = 'zip -r {0} {1}'.format(target,''.join(source))

print('Zip command is :',zip_command)

if os.system(zip_command) == 0:
    print('Success :',target)
else:
    print('Back up FAILED')

输出信息

Zip command is : zip -r /Users/let/Desktop/saveFile/20171227144205.zip /Users/let/Desktop/UI图/File
adding: Users/let/Desktop/UI图/File/ (stored 0%)
adding: Users/let/Desktop/UI图/File/UI设计征求意见稿.docx (deflated 18%)
adding: Users/let/Desktop/UI图/File/UI问题整理-ios.xlsx (deflated 22%)
Success : /Users/let/Desktop/saveFile/20171227144205.zip

在路径targetSource下就会有一个20171227144205.zip 即表示成功。


image.png

image.png

备份文件并打包成 zip-第二版

指定文件目录,指定文件子目录和压缩文件名
示例代码

import os
import time

# 1.需要备份的文件路径,必须如下格式['@#$%']
source = [' /Users/let/Desktop/UI图/File']

# 2.备份文件存储位置路径
targetSource = '/Users/let/Desktop/saveFile2'

# 如果系统没有指定的目录,则创建目录
if not os.path.exists(targetSource):
    os.mkdir(targetSource)

# 3.将备份文件打包压缩 zip 文件,然后以当前时间命名 zip 文件名
# target = targetSource + os.sep + \
         # time.strftime('%Y%m%d%H%M%S') + '.zip'

# 当前日期作为主备份目录下的子目录名称
today = targetSource + os.sep + time.strftime('%Y%m%d')

# 如果系统没有指定的目录,则创建目录
if not os.path.exists(today):
    os.mkdir(today)

# 当前时间作为备份文件的文件名称
file = time.strftime('%H%M%S')
# zip 文件名称格式
target = today + os.sep + file + '.zip'

# 4.使用 zip 命令将文件打包成 zip 格式
zip_command = 'zip -r {0} {1}'.format(target,''.join(source))
#zip_command 就表示在终端使用的命令行

print('Zip command is :',zip_command)

if os.system(zip_command) == 0:
    print('Success :',target)
else:
    print('Back up FAILED')

输出信息

Zip command is : zip -r /Users/let/Desktop/saveFile2/20171227/150849.zip /Users/let/Desktop/UI图/File
adding: Users/let/Desktop/UI图/File/ (stored 0%)
adding: Users/let/Desktop/UI图/File/UI设计征求意见稿.docx (deflated 18%)
adding: Users/let/Desktop/UI图/File/UI问题整理-ios.xlsx (deflated 22%)
Success : /Users/let/Desktop/saveFile2/20171227/150849.zip

备份文件并打包成 zip-第三版

指定文件目录,指定文件子目录;采用输入压缩文件名,输入为空为默认
示例代码

import os
import time

# 1.需要备份的文件路径,必须如下格式['@#$%']
source = [' /Users/let/Desktop/UI图/File']

# 2.备份文件存储位置路径
targetSource = '/Users/let/Desktop/saveFile2'

# 如果系统没有指定的目录,则创建目录
if not os.path.exists(targetSource):
    os.mkdir(targetSource)


# 3.将备份文件打包压缩 zip 文件,然后以当前时间命名 zip 文件名
# target = targetSource + os.sep + \
         # time.strftime('%Y%m%d%H%M%S') + '.zip'

# 当前日期作为主备份目录下的子目录名称
today = targetSource + os.sep + time.strftime('%Y%m%d')

# 如果系统没有指定的目录,则创建目录
if not os.path.exists(today):
    os.mkdir(today)

# 当前时间作为备份文件的文件名称
file = time.strftime('%H%M%S')


# 用户手动输入文件名称
inputFile = input('Enter a file name-->')

if len(inputFile) == 0:
    # zip 文件名称格式
    target = today + os.sep + file + '.zip'
else:
    # 建议 清除一下输入文字的空格
    # target = today + os.sep + \
    #          inputFile + '.zip'
    target = today + os.sep + file + '-' + \
        inputFile.replace(' ','-') + '.zip'

# 4.使用 zip 命令将文件打包成 zip 格式
zip_command = 'zip -r {0} {1}'.format(target,''.join(source))
#zip_command 就表示在终端使用的命令行

print('Zip command is :',zip_command)

if os.system(zip_command) == 0:
    print('Success :',target)
else:
    print('Back up FAILED')

输出信息

Enter a file name-->hello word
Zip command is : zip -r /Users/let/Desktop/saveFile2/20171227/-hello-word.zip /Users/let/Desktop/UI图/File
adding: Users/let/Desktop/UI图/File/ (stored 0%)
adding: Users/let/Desktop/UI图/File/UI设计征求意见稿.docx (deflated 18%)
adding: Users/let/Desktop/UI图/File/UI问题整理-ios.xlsx (deflated 22%)
Success : /Users/let/Desktop/saveFile2/20171227/-hello-word.zip

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,348评论 19赞 139
  • 做你应的事,能有什么结果则在其次。——赫伯特 让自己内心藏着一条巨龙,即是一种苦刑,也是一种乐趣。——雨果 人的志...
    新阁阅读 183评论 0赞 0
  • 生活中,我们经常遇到的困惑是:不知道我们热爱什么?不知道自己的天赋是什么? 于是我带着这个好奇,2016年6月25...
    Kid小云阅读 318评论 0赞 0
  • 20171123日 班班分享 护城河有? 1. 无形资产:品牌、专利、政府授权 2. 转换成本 3. 网络效应 4...
    简单在心阅读 915评论 0赞 2
  • 2017年的第一场雪意外的飘落在岁首的初春里。披着暮色,像一个在16年冬季里爽约的情人,带着一丝羞怯、一丝...
    潘国旭阅读 703评论 4赞 2

友情链接更多精彩内容