파일 백업-python 3 폴 더 아래 모든 파일 압축 처리

1547 단어 Pythonzipfile백업
파일 백업,Python 3 을 사용 하여 zip 파일 을 처리 합 니 다.Python 은 zipfile 을 사용 하여 간단 한 백업 을 합 니 다.

# !/usr/bin/python3
# coding:utf-8
# Filename: backup_ver1.py
import os,time,zipfile

def createZip(filePath,savePath,note = ''):
    '''
               zip   。
    :param filePath:      
    :param savePath:     
    :param note:       
    :return:
    '''
    today = time.strftime('%Y%m%d')
    now = time.strftime('%H%M%S')
    fileList=[]
    if not os.path.exists(today):
        os.mkdir(today)
        print('mkdir successful')
    if len(note) == 0:
        target = savePath + os.sep + today + os.sep + now + '.zip'
    else:
        target = savePath + os.sep + today + os.sep + now + '_' + note + '.zip'
    newZip = zipfile.ZipFile(target,'w')
    for dirpath,dirnames,filenames in os.walk(filePath):
        for filename in filenames:
            fileList.append(os.path.join(dirpath,filename))
    for tar in fileList:
        newZip.write(tar,tar[len(filePath):])#tar      ,tar[len(filePath)]       
    newZip.close()
    print('backup to',target)

4.567913.함 수 를 정의 한 후에 호출 합 니 다.
 
  
def unZip(filePath,unzipPath):
    '''
      zip       
    :param filePath:      
    :param unzipPath:     
    :return: 
    '''
    file = zipfile.ZipFile(filePath)
    file.extractall(unzipPath)
    print('unzip successfully to',unzipPath)

좋은 웹페이지 즐겨찾기