python實現(xiàn)批量壓縮指定目錄下的文件夾
更新時間:2023年08月25日 09:59:41 作者:谷雨之際
這篇文章主要介紹了利用Python實現(xiàn)批量壓縮指定目錄下的文件夾的示例代碼,文中代碼示例講解詳細,感興趣的小伙伴快跟隨小編一起動手試一試
語言:python 3
用法:選擇目錄,對該目錄下的文件夾分別壓縮,生成同名壓縮文件,并保存到該目錄下。
import os
import shutil
import zipfile
from tkinter import Tk
from tkinter import filedialog
root = Tk()
root.withdraw()
directory = filedialog.askdirectory(title="選擇目錄")
def zip_folders(directory):
for root, dirs, files in os.walk(directory):
for dir_name in dirs:
folder_path = os.path.join(root, dir_name)
zip_path = f"{folder_path}"
shutil.make_archive(zip_path, 'zip', folder_path)
print(f"已壓縮文件夾: {zip_path}")
zip_folders(directory)
zip_folders(directory)到此這篇關于python實現(xiàn)批量壓縮指定目錄下的文件夾的文章就介紹到這了,更多相關python批量壓縮指定文件夾內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python之標點符號string.punctuation的使用
Python的string模塊提供了一個方便的屬性string.punctuation,其中包含所有ASCII標點符號字符,這使得在處理和識別字符串中的標點符號時非常有用,可以通過簡單的in關鍵字來檢測字符是否為標點2024-09-09

