備份文件并打包成 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 即表示成功。


備份文件并打包成 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