“Buat zipfile dari direktori py” Kode Jawaban

Folder Zip Python

import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
DigitalBorder

Folder Zip Python

import os
import zipfile

def zip_directory(folder_path, zip_path):
    with zipfile.ZipFile(zip_path, mode='w') as zipf:
        len_dir_path = len(folder_path)
        for root, _, files in os.walk(folder_path):
            for file in files:
                file_path = os.path.join(root, file)
                zipf.write(file_path, file_path[len_dir_path:])
                
zip_directory('C:/FolderToZip', 'C:/Folder.zip')
Gentle Gazelle

Buat zipfile dari direktori py

import zipfile
filePaths = [] # Make an array string with all paths to files
for root, directories, files in os.walk("MyDirectoryPath"): # Scans for all subfolders and files in MyDirectoryPath
        for filename in files: # loops for every file
            filePath = os.path.join(root, filename) # Joins both the directory and the file name
            filePaths.append(filePath) # appends to the array
z = zipfile.ZipFile("MyDirectoryPathWithZipExt.zip", 'w')
with z:
    for file in filePaths: # Loops for all files in array
        z.write(file) # Writes file to MyDirectoryPathWithZipExt.zip
AcaiBerii

Jawaban yang mirip dengan “Buat zipfile dari direktori py”

Pertanyaan yang mirip dengan “Buat zipfile dari direktori py”

Lebih banyak jawaban terkait untuk “Buat zipfile dari direktori py” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya