Hướng dẫn zip folder python - thư mục zip python

Nối tiếp tutorial về lập trình Python, hôm nay chúng ta sẽ học cách tạo ra file zip bằng cách sử dụng Python, thông qua thư viện zipfile. Thư viện này là build-in của Python 3 nên các bạn không cần phải thực hiện cài đặt.

Nội dung chính ShowShow

  • Tạo File Zip sử dụng Python
  • Làm thế nào tôi có thể tạo một kho lưu trữ zip của một cấu trúc thư mục trong Python?
  • Sử dụng Python từ vỏ
  • Zipping Up Python (hoặc không chỉ muốn cha mẹ):
  • Zipping a Python Ứng dụng:

Tạo File Zip sử dụng Python

Làm thế nào tôi có thể tạo một kho lưu trữ zip của một cấu trúc thư mục trong Python?

import zipfile

Sử dụng Python từ vỏ

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)

Zipping Up Python (hoặc không chỉ muốn cha mẹ):

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')

Zipping a Python Ứng dụng:

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression

Đầu tiên là import thư viện filezip

# Don't forget to close the file!
zf.close()

Lựa chọn mode để nén file zip

import zipfile

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression)

# Don't forget to close the file!
zf.close()

Tạo file Zip với đường dẫn và mode (w – write, a – append)

Thêm file vào file zip vừa tạo

Hướng dẫn zip folder python - thư mục zip python

Close file

Vậy đoạn code đầy đủ sẽ là:

Kết quả 12 years, 10 months ago

Sau khi chạy đoạn code trên chúng ta đã tạo thành công file zip 628k times

777

Đây là chương trình tạo file zip đơn giản, các bạn muốn làm nhiều thư hơn như zip folder, tạo mật khẩu cho file zip thì vui lòng tham khảo hướng dẫn sử dụng thư viện zipfile ở đây.
Learn more.

Làm thế nào tôi có thể tạo một kho lưu trữ zip của một cấu trúc thư mục trong Python?

Sử dụng Python từ vỏ

Zipping Up Python (hoặc không chỉ muốn cha mẹ):15 gold badges38 silver badges55 bronze badges

Zipping a Python Ứng dụng:Dec 6, 2009 at 11:12

4

Đầu tiên là import thư viện filezip

Lựa chọn mode để nén file zip

Tạo file Zip với đường dẫn và mode (w – write, a – append)

Thêm file vào file zip vừa tạo

Close file

Vậy đoạn code đầy đủ sẽ là:4 gold badges37 silver badges44 bronze badges

Kết quảSep 3, 2014 at 17:26

Sau khi chạy đoạn code trên chúng ta đã tạo thành công file zipcrdavis

Đây là chương trình tạo file zip đơn giản, các bạn muốn làm nhiều thư hơn như zip folder, tạo mật khẩu cho file zip thì vui lòng tham khảo hướng dẫn sử dụng thư viện zipfile ở đây.3 gold badges13 silver badges9 bronze badges

22

Nguồn vinasupport.com

import os
import zipfile
    
def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file), 
                       os.path.relpath(os.path.join(root, file), 
                                       os.path.join(path, '..')))

with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    zipdir('tmp/', zipf)

Đã hỏi 12 năm, 10 tháng trước 12 years, 10 months ago

Đã xem 628k lần 628k times 25 gold badges186 silver badges226 bronze badges

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.Learn more.Dec 6, 2009 at 11:23

TomerikooMark Byers

16.7K15 Huy hiệu vàng38 Huy hiệu bạc55 Huy hiệu Đồng15 gold badges38 silver badges55 bronze badges188 gold badges1552 silver badges1440 bronze badges

15

Đã hỏi ngày 6 tháng 12 năm 2009 lúc 11:12Dec 6, 2009 at 11:12

Cách dễ nhất là sử dụng

# Don't forget to close the file!
zf.close()
5. Nó hỗ trợ cả định dạng zip và tar.
import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
Dec 6, 2009 at 11:28

Nếu bạn cần làm một cái gì đó phức tạp hơn so với việc hạ gục toàn bộ thư mục (chẳng hạn như bỏ qua một số tệp nhất định), thì bạn sẽ cần đào vào mô -đun Ben James

# Don't forget to close the file!
zf.close()
6 như những người khác đã đề xuất.26 gold badges191 silver badges155 bronze badges

5

Làm thế nào tôi có thể tạo một kho lưu trữ zip của một cấu trúc thư mục trong Python?

Sử dụng Python từ vỏ

Zipping Up Python (hoặc không chỉ muốn cha mẹ):

Zipping a Python Ứng dụng:

Đầu tiên là import thư viện filezip

Lựa chọn mode để nén file zip

Tạo file Zip với đường dẫn và mode (w – write, a – append)

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
0

Sử dụng Python từ vỏ

Thêm file vào file zip vừa tạo

# Don't forget to close the file!
zf.close()
6:
# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
1

Close file

Vậy đoạn code đầy đủ sẽ là:

Zipping Up Python (hoặc không chỉ muốn cha mẹ):

Kết quả

Sau khi chạy đoạn code trên chúng ta đã tạo thành công file zip

Đây là chương trình tạo file zip đơn giản, các bạn muốn làm nhiều thư hơn như zip folder, tạo mật khẩu cho file zip thì vui lòng tham khảo hướng dẫn sử dụng thư viện zipfile ở đây.

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
3

Nguồn vinasupport.com

Zipping a Python Ứng dụng:

Đầu tiên là import thư viện filezip

Lựa chọn mode để nén file zip

Tạo file Zip với đường dẫn và mode (w – write, a – append)Mar 31, 2016 at 18:57

0

Thêm file vào file zip vừa tạo

import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
0.
# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
5

Close fileJun 13, 2013 at 6:57

Vậy đoạn code đầy đủ sẽ là:George V. Reilly

Kết quả7 gold badges41 silver badges38 bronze badges

1

Sau khi chạy đoạn code trên chúng ta đã tạo thành công file zip

  • Arg đầu tiên: Tên tệp của tệp zip/tar kết quả,
  • Arg thứ 2: zip/tar,
  • Arg thứ 3: dir_name

Code:

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
6

Qback

3.7352 Huy hiệu vàng22 Huy hiệu bạc35 Huy hiệu Đồng2 gold badges22 silver badges35 bronze badges2 gold badges22 silver badges35 bronze badges

Đã trả lời ngày 12 tháng 10 năm 2018 lúc 9:46Oct 12, 2018 at 9:46Oct 12, 2018 at 9:46

Vadiraj s jvadiraj s jVadiraj S JVadiraj S J

6119 Huy hiệu bạc17 Huy hiệu đồng9 silver badges17 bronze badges9 silver badges17 bronze badges

2

Python hiện đại (3.6+) sử dụng mô-đun

import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
1 để xử lý các đường dẫn giống như OOP và
import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
2 để làm toàn cầu đệ quy. Theo như tôi có thể nói, điều này tương đương với câu trả lời của George V. Reilly: Zips với nén, phần tử hàng đầu là một thư mục, giữ cho các dirs trống, sử dụng các đường dẫn tương đối.
# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
7

Lưu ý: Như các gợi ý loại tùy chọn chỉ ra,

import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
3 không thể là đối tượng đường dẫn (sẽ được sửa trong 3.6.2+).

Đã trả lời ngày 31 tháng 3 năm 2017 lúc 13:01Mar 31, 2017 at 13:01Mar 31, 2017 at 13:01

monk-timemonk-timemonk-timemonk-time

1.87212 Huy hiệu bạc17 Huy hiệu đồng12 silver badges17 bronze badges12 silver badges17 bronze badges

1

Với mô -đun Python 3.9,

import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
1 &
# Don't forget to close the file!
zf.close()
6, bạn có thể tạo một tệp zip từ bất cứ đâu trong hệ thống.
# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
8

Nó là gọn gàng, gõ và có ít mã hơn.

Đã trả lời ngày 17 tháng 8 năm 2021 lúc 12:05Aug 17, 2021 at 12:05Aug 17, 2021 at 12:05

JD Solankijd SolankiJD SolankiJD Solanki

7106 Huy hiệu bạc15 Huy hiệu Đồng6 silver badges15 bronze badges6 silver badges15 bronze badges

Để thêm nén vào tệp zip kết quả, hãy xem liên kết này.

Bạn cần phải thay đổi:

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
9

đến

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
0

Philshem

24.2K7 Huy hiệu vàng59 Huy hiệu bạc123 Huy hiệu đồng7 gold badges59 silver badges123 bronze badges7 gold badges59 silver badges123 bronze badges

Đã trả lời ngày 27 tháng 4 năm 2013 lúc 17:14Apr 27, 2013 at 17:14Apr 27, 2013 at 17:14

E Smithe SmithE SmithE Smith

1371 Huy hiệu bạc3 Huy hiệu đồng1 silver badge3 bronze badges1 silver badge3 bronze badges

0

Tôi đã thực hiện một số thay đổi đối với mã được đưa ra bởi Mark Byers. Chức năng dưới đây cũng sẽ thêm các thư mục trống nếu bạn có chúng. Các ví dụ sẽ làm cho nó rõ ràng hơn đường dẫn được thêm vào zip.

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
1

Trên đây là một chức năng đơn giản nên làm việc cho các trường hợp đơn giản. Bạn có thể tìm thấy lớp thanh lịch hơn trong ý chính của tôi: https://gist.github.com/eccenux/17526123107CA0AC28E6

Đã trả lời ngày 10 tháng 6 năm 2013 lúc 9:28Jun 10, 2013 at 9:28Jun 10, 2013 at 9:28

NuxnuxNuxNux

8.4835 Huy hiệu vàng55 Huy hiệu bạc69 Huy hiệu Đồng5 gold badges55 silver badges69 bronze badges5 gold badges55 silver badges69 bronze badges

3

Tôi có một ví dụ mã khác có thể giúp, sử dụng python3, pathlib và zipfile. Nó sẽ hoạt động trong bất kỳ hệ điều hành nào.

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
2

Đã trả lời ngày 2 tháng 10 năm 2015 lúc 10:03Oct 2, 2015 at 10:03Oct 2, 2015 at 10:03

Để giữ lại hệ thống phân cấp thư mục theo thư mục phụ huynh để được lưu trữ:

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
3

Nếu bạn muốn, bạn có thể thay đổi điều này để sử dụng

import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
1 để tìm thấy tệp.

Đã trả lời ngày 9 tháng 7 năm 2019 lúc 8:48Jul 9, 2019 at 8:48Jul 9, 2019 at 8:48

Ryanjdillonryanjdillonryanjdillonryanjdillon

16.5k9 Huy hiệu vàng82 Huy hiệu bạc104 Huy hiệu đồng9 gold badges82 silver badges104 bronze badges9 gold badges82 silver badges104 bronze badges

Nếu bạn muốn có một chức năng như thư mục nén của bất kỳ trình quản lý tệp đồ họa phổ biến nào bạn có thể sử dụng mã sau, nó sẽ sử dụng mô -đun ZipFile. Sử dụng mã này, bạn sẽ có tệp zip với đường dẫn làm thư mục gốc của nó.

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
4

Đã trả lời ngày 21 tháng 3 năm 2016 lúc 13:40Mar 21, 2016 at 13:40Mar 21, 2016 at 13:40

VGe0rgeVGe0rgeVGe0rgeVGe0rge

1.0103 huy hiệu vàng15 Huy hiệu bạc18 Huy hiệu đồng3 gold badges15 silver badges18 bronze badges3 gold badges15 silver badges18 bronze badges

Vì vậy, nhiều câu trả lời ở đây và tôi hy vọng tôi có thể đóng góp với phiên bản của riêng mình, dựa trên câu trả lời ban đầu (nhân tiện), nhưng với góc độ đồ họa hơn, cũng sử dụng bối cảnh cho mỗi thiết lập

# Don't forget to close the file!
zf.close()
6 và sắp xếp
import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
8 có một đầu ra đặt hàng.

Có các thư mục này và các tệp chúng (trong số các thư mục khác), tôi muốn tạo một

import zipfile

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression)

# Don't forget to close the file!
zf.close()
7 cho mỗi thư mục
import os
import zipfile
    
def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file), 
                       os.path.relpath(os.path.join(root, file), 
                                       os.path.join(path, '..')))

with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    zipdir('tmp/', zipf)
0:
# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
5

Đây là những gì tôi đã áp dụng, với các nhận xét để hiểu rõ hơn về quy trình.

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
6

Về cơ bản, đối với mỗi lần lặp qua

import os
import zipfile
    
def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file), 
                       os.path.relpath(os.path.join(root, file), 
                                       os.path.join(path, '..')))

with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    zipdir('tmp/', zipf)
1, tôi đang mở bối cảnh cho thiết lập
# Don't forget to close the file!
zf.close()
6 và sau đó, lặp lại trên
import os
import zipfile
    
def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file), 
                       os.path.relpath(os.path.join(root, file), 
                                       os.path.join(path, '..')))

with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    zipdir('tmp/', zipf)
3, đây là một ____74 của các tệp từ thư mục
import os
import zipfile
    
def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file), 
                       os.path.relpath(os.path.join(root, file), 
                                       os.path.join(path, '..')))

with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    zipdir('tmp/', zipf)
5, hình thành đường dẫn tương đối cho mỗi tệp dựa trên thư mục
import os
import zipfile
    
def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file), 
                       os.path.relpath(os.path.join(root, file), 
                                       os.path.join(path, '..')))

with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    zipdir('tmp/', zipf)
5 hiện tại đến bối cảnh
# Don't forget to close the file!
zf.close()
6 đang chạy.

Và đầu ra được trình bày như thế này:

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
7

Để xem nội dung của mỗi thư mục

import zipfile

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression)

# Don't forget to close the file!
zf.close()
7, bạn có thể sử dụng lệnh
import os
import zipfile
    
def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file), 
                       os.path.relpath(os.path.join(root, file), 
                                       os.path.join(path, '..')))

with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    zipdir('tmp/', zipf)
9:
# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
8

Đã trả lời ngày 5 tháng 9 năm 2019 lúc 13:10Sep 5, 2019 at 13:10Sep 5, 2019 at 13:10

Ivanleonczivanleonczivanleonczivanleoncz

8.0974 Huy hiệu vàng53 Huy hiệu bạc48 Huy hiệu đồng4 gold badges53 silver badges48 bronze badges4 gold badges53 silver badges48 bronze badges

Bạn có thể muốn xem mô -đun

# Don't forget to close the file!
zf.close()
6; Có tài liệu tại http://docs.python.org/l Library/zipfile.html.

Bạn cũng có thể muốn

import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
8 lập chỉ mục cấu trúc thư mục.

Đã trả lời ngày 6 tháng 12 năm 2009 lúc 11:17Dec 6, 2009 at 11:17Dec 6, 2009 at 11:17

me_andme_andme_andme_and

14.8K7 Huy hiệu vàng61 Huy hiệu bạc96 Huy hiệu Đồng7 gold badges61 silver badges96 bronze badges7 gold badges61 silver badges96 bronze badges

Dưới đây là một biến thể của câu trả lời được đưa ra bởi Nux phù hợp với tôi:

# create the zip file first parameter path/name, second mode
zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')
9

Đã trả lời ngày 30 tháng 7 năm 2014 lúc 23:13Jul 30, 2014 at 23:13Jul 30, 2014 at 23:13

M Katzm KatzM KatzM Katz

4.8723 Huy hiệu vàng41 Huy hiệu bạc64 Huy hiệu Đồng3 gold badges41 silver badges64 bronze badges3 gold badges41 silver badges64 bronze badges

Hãy thử cái dưới đây .it đã làm việc cho tôi.. .

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
0

Đã trả lời ngày 23 tháng 4 năm 2015 lúc 11:18Apr 23, 2015 at 11:18Apr 23, 2015 at 11:18

ChandrachandraChandraChandra

1011 huy hiệu vàng3 Huy hiệu bạc12 Huy hiệu đồng1 gold badge3 silver badges12 bronze badges1 gold badge3 silver badges12 bronze badges

Để cho linh hoạt hơn, ví dụ: Chọn Thư mục/Tệp theo tên Sử dụng:

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
1

Cho một cây tập tin:

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
2

Bạn có thể, ví dụ: Chỉ chọn

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
12 và
# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
13:
# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
3

Hoặc chỉ

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
14 trong thư mục gọi tập lệnh và thêm mọi thứ từ đó:
# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
4

Đã trả lời ngày 25 tháng 9 năm 2018 lúc 13:32Sep 25, 2018 at 13:32Sep 25, 2018 at 13:32

pbnpbnpbnpbn

2.2161 Huy hiệu vàng21 Huy hiệu bạc37 Huy hiệu đồng1 gold badge21 silver badges37 bronze badges1 gold badge21 silver badges37 bronze badges

1

Giả sử bạn muốn zip tất cả các thư mục (thư mục phụ) trong thư mục hiện tại.

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
5

Uli Köhler

12.6K15 Huy hiệu vàng65 Huy hiệu bạc114 Huy hiệu đồng15 gold badges65 silver badges114 bronze badges15 gold badges65 silver badges114 bronze badges

Đã trả lời ngày 9 tháng 4 năm 2019 lúc 8:25Apr 9, 2019 at 8:25Apr 9, 2019 at 8:25

SushilinuxsushilinuxSushilinuxSushilinux

7007 Huy hiệu bạc21 Huy hiệu Đồng7 silver badges21 bronze badges7 silver badges21 bronze badges

Zip một tệp hoặc một cây (một thư mục và các hướng phụ của nó).

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
6

Để nối vào một kho lưu trữ hiện có, Pass

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
15, để tạo một kho lưu trữ mới
# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
16 (mặc định ở trên). Vì vậy, giả sử bạn muốn gói 3 cây thư mục khác nhau trong cùng một kho lưu trữ.
# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
7

Đã trả lời ngày 17 tháng 9 năm 2020 lúc 5:50Sep 17, 2020 at 5:50Sep 17, 2020 at 5:50

Michael Ekokamichael EkokaMichael EkokaMichael Ekoka

18.3k10 Huy hiệu vàng74 Huy hiệu bạc78 Huy hiệu đồng10 gold badges74 silver badges78 bronze badges10 gold badges74 silver badges78 bronze badges

Một giải pháp sử dụng

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
17, độc lập với HĐH được sử dụng:
# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
8

Đã trả lời ngày 5 tháng 1 năm 2021 lúc 9:09Jan 5, 2021 at 9:09Jan 5, 2021 at 9:09

AlexalexAlexAlex

2.1962 Huy hiệu vàng26 Huy hiệu bạc39 Huy hiệu Đồng2 gold badges26 silver badges39 bronze badges2 gold badges26 silver badges39 bronze badges

Cách rõ ràng để đi là đi với Shutil, như câu trả lời hàng đầu thứ hai đã nói như vậy, nhưng nếu bạn vẫn muốn đi với Zipfile vì một số lý do, và nếu bạn gặp một số rắc rối khi làm điều đó (như ERR 13 trong Windows, v.v.) , Bạn có thể sử dụng bản sửa lỗi này:

# Add file to the zip file
# first parameter file to zip, second filename in zip
zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
9

Cái này lặp đi lặp lại một cách đệ quy thông qua mọi bộ lọc/tệp phụ trong thư mục đã cho của bạn và ghi chúng vào tệp zip thay vì cố gắng trực tiếp zip một thư mục.

Đã trả lời ngày 10 tháng 3 năm 2021 lúc 5:09Mar 10, 2021 at 5:09Mar 10, 2021 at 5:09

Đây là một cách tiếp cận hiện đại, sử dụng pathlib và người quản lý bối cảnh. Đặt các tệp trực tiếp vào zip, thay vì trong một thư mục con.

# Don't forget to close the file!
zf.close()
0

Đã trả lời ngày 14 tháng 12 năm 2016 lúc 21:50Dec 14, 2016 at 21:50Dec 14, 2016 at 21:50

Rùa là những con cuteturtles rất dễ thươngTurtles Are CuteTurtles Are Cute

2.9666 huy hiệu vàng28 Huy hiệu bạc37 Huy hiệu đồng6 gold badges28 silver badges37 bronze badges6 gold badges28 silver badges37 bronze badges

Tôi đã chuẩn bị một chức năng bằng cách hợp nhất giải pháp của Mark Byers với các bình luận của Reimund và Morten Zilmer (đường dẫn tương đối và bao gồm các thư mục trống). Như một thông lệ tốt nhất,

# Select the compression mode ZIP_DEFLATED for compression
# or zipfile.ZIP_STORED to just store the file
try:
    import zlib
    compression = zipfile.ZIP_DEFLATED
except Exception as ex:
    compression = zipfile.ZIP_STORED
    print(ex)
18 được sử dụng trong việc xây dựng tệp của Zipfile.

Hàm cũng chuẩn bị một tên tệp zip mặc định với tên thư mục bị nén và tiện ích mở rộng '.zip'. Do đó, nó chỉ hoạt động với một đối số: thư mục nguồn được nén.

# Don't forget to close the file!
zf.close()
1

Đã trả lời ngày 24 tháng 12 năm 2016 lúc 20:32Dec 24, 2016 at 20:32Dec 24, 2016 at 20:32

Gürol Canbekgürol CanbekGürol CanbekGürol Canbek

1,0081 Huy hiệu vàng14 Huy hiệu bạc20 Huy hiệu Đồng1 gold badge14 silver badges20 bronze badges1 gold badge14 silver badges20 bronze badges

# Don't forget to close the file!
zf.close()
2

Đã trả lời ngày 15 tháng 6 năm 2017 lúc 6:24Jun 15, 2017 at 6:24Jun 15, 2017 at 6:24

Chà, sau khi đọc các đề xuất, tôi đã đưa ra một cách rất giống nhau hoạt động với 2.7.x mà không tạo tên thư mục "hài hước" (tên giống như tuyệt đối) và sẽ chỉ tạo thư mục được chỉ định bên trong zip.

Hoặc chỉ trong trường hợp bạn cần zip của mình để chứa một thư mục bên trong với nội dung của thư mục đã chọn.

# Don't forget to close the file!
zf.close()
3

Đã trả lời ngày 15 tháng 8 năm 2018 lúc 21:27Aug 15, 2018 at 21:27Aug 15, 2018 at 21:27

XedretxedretXedretXedret

1.77317 Huy hiệu bạc24 Huy hiệu đồng17 silver badges24 bronze badges17 silver badges24 bronze badges

Chức năng để tạo tệp zip.

# Don't forget to close the file!
zf.close()
4

Đã trả lời ngày 19 tháng 11 năm 2018 lúc 3:55Nov 19, 2018 at 3:55Nov 19, 2018 at 3:55

Sushhsushhsushhsushh

Huy hiệu bạc 11511 silver badge10 bronze badges1 silver badge10 bronze badges

2