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設(shè)計征求意見稿.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'

# 如果系統(tǒng)沒有指定的目錄,則創(chuàng)建目錄
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')

# 如果系統(tǒng)沒有指定的目錄,則創(chuàng)建目錄
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設(shè)計征求意見稿.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'

# 如果系統(tǒng)沒有指定的目錄,則創(chuàng)建目錄
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')

# 如果系統(tǒng)沒有指定的目錄,則創(chuàng)建目錄
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設(shè)計征求意見稿.docx (deflated 18%)
adding: Users/let/Desktop/UI圖/File/UI問題整理-ios.xlsx (deflated 22%)
Success : /Users/let/Desktop/saveFile2/20171227/-hello-word.zip

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評論 19 139
  • 做你應(yīng)的事,能有什么結(jié)果則在其次?!詹?讓自己內(nèi)心藏著一條巨龍,即是一種苦刑,也是一種樂趣?!旯?人的志...
    新閣閱讀 150評論 0 0
  • 生活中,我們經(jīng)常遇到的困惑是:不知道我們熱愛什么?不知道自己的天賦是什么? 于是我?guī)е@個好奇,2016年6月25...
    Kid小云閱讀 302評論 0 0
  • 20171123日 班班分享 護城河有? 1. 無形資產(chǎn):品牌、專利、政府授權(quán) 2. 轉(zhuǎn)換成本 3. 網(wǎng)絡(luò)效應(yīng) 4...
    簡單在心閱讀 893評論 0 2
  • 2017年的第一場雪意外的飄落在歲首的初春里。披著暮色,像一個在16年冬季里爽約的情人,帶著一絲羞怯、一絲...
    潘國旭閱讀 669評論 4 2

友情鏈接更多精彩內(nèi)容