壓縮與解壓指定目錄下的指定檔案

2021-07-10 10:27:10 字數 791 閱讀 1991

#解壓檔案

import gzip,zipfile

def zip_to_unzip(self,filename,filedir):

#引數:檔名 解壓後存放檔案的目錄

is_zip = zipfile.is_zipfile(filename)

if is_zip:

fz = zipfile.zipfile(filename,'r')

for file in fz.namelist():

fz.extract(file,filedir)

函式呼叫:

zip_to_unzip("d:/cecproject/win_7.xml.zip","d:/cecproject/") 解壓到當前目錄

#壓縮檔案 將指定目錄下的指定檔案壓縮成zip檔案

def file_to_zip(self,file_path,file_suffix):

#引數:檔案所在路徑 檔案字尾

os.chdir(file_path)

sc_file = self.get_file_name(file_path, file_suffix)

zip_file = sc_file + ".zip"

file_name = zipfile.zipfile(zip_file,"w",zipfile.zip_deflated)

file_name.write(sc_file)

file_name.close()

根據字尾名將指定目錄下的檔案打包成zip檔案

刪除指定目錄下指定字尾的檔案

定時清除計畫任務日誌檔案,避免占用太大磁碟空間 folderpath www server log 要操作的目錄 deltype array log foreach deltype as file type param path資料夾絕對路徑 file type待刪除檔案的字尾名 return vo...

python刪除指定目錄下的指定檔案和資料夾

具體 coding utf 8 import os,sys,platform class removetagfile object path none def removefile self,path,remove list,retain list path後面要跟 self.path path s...

指定檔案目錄下 遞迴只拷貝檔案到指定目錄

public static void main string args public final static string linux linux public final static string linux windows 讀取源檔案目錄 讀取全部檔案 存放在list裡面 param sou...