Python tạo zip bằng mật khẩu

Python's

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 là một mô-đun thư viện tiêu chuẩn nhằm thao tác các tệp ZIP. Định dạng tệp này là một tiêu chuẩn công nghiệp được áp dụng rộng rãi khi lưu trữ và nén dữ liệu kỹ thuật số. Bạn có thể sử dụng nó để đóng gói một số tệp liên quan lại với nhau. Nó cũng cho phép bạn giảm kích thước tệp và tiết kiệm dung lượng ổ đĩa. Quan trọng nhất, nó tạo điều kiện trao đổi dữ liệu qua mạng máy tính

Biết cách tạo, đọc, viết, điền, trích xuất và liệt kê các tệp ZIP bằng mô-đun

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 là một kỹ năng hữu ích cần có với tư cách là nhà phát triển Python hoặc kỹ sư DevOps

Trong hướng dẫn này, bạn sẽ học cách

  • Đọc, viết và giải nén tệp từ tệp ZIP bằng Python's
    >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    3
  • Đọc siêu dữ liệu về nội dung của tệp ZIP bằng cách sử dụng
    >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    3
  • Sử dụng
    >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    3 để thao tác các tệp thành viên trong các tệp ZIP hiện có
  • Tạo tệp ZIP mới để lưu trữ và nén tệp

Nếu bạn thường xử lý các tệp ZIP, thì kiến ​​thức này có thể giúp hợp lý hóa quy trình làm việc của bạn để xử lý các tệp của bạn một cách tự tin

Để tận dụng tối đa hướng dẫn này, bạn nên biết kiến ​​thức cơ bản về làm việc với tệp, sử dụng câu lệnh

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8, xử lý đường dẫn hệ thống tệp với
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
9 và làm việc với các lớp và lập trình hướng đối tượng

Để nhận các tệp và kho lưu trữ mà bạn sẽ sử dụng để mã hóa các ví dụ trong hướng dẫn này, hãy nhấp vào liên kết bên dưới

Nhận tài liệu. Nhấp vào đây để nhận bản sao của các tệp và kho lưu trữ mà bạn sẽ sử dụng để chạy các ví dụ trong hướng dẫn về tệp zip này

Bắt đầu với tệp ZIP

Các tệp ZIP là một công cụ nổi tiếng và phổ biến trong thế giới kỹ thuật số ngày nay. Các tệp này khá phổ biến và được sử dụng rộng rãi để trao đổi dữ liệu đa nền tảng qua mạng máy tính, đặc biệt là Internet

Bạn có thể sử dụng các tệp ZIP để nhóm các tệp thông thường lại với nhau thành một kho lưu trữ duy nhất, nén dữ liệu của bạn để tiết kiệm dung lượng ổ đĩa, phân phối các sản phẩm kỹ thuật số của bạn, v.v. Trong hướng dẫn này, bạn sẽ học cách thao tác với các tệp ZIP bằng mô-đun

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 của Python

Vì thuật ngữ xung quanh các tệp ZIP đôi khi có thể gây nhầm lẫn, hướng dẫn này sẽ tuân theo các quy ước sau về thuật ngữ

Thuật ngữÝ nghĩaTệp ZIP, kho lưu trữ ZIP hoặc kho lưu trữMột tệp vật lý sử dụng định dạng tệp ZIPTệpMột tệp máy tính thông thườngTệp thành viênMột tệp là một phần của tệp ZIP hiện có

Ghi nhớ rõ ràng các thuật ngữ này sẽ giúp bạn tránh nhầm lẫn khi đọc qua các phần sắp tới. Bây giờ bạn đã sẵn sàng để tiếp tục tìm hiểu cách thao tác hiệu quả với các tệp ZIP trong mã Python của mình

Loại bỏ các quảng cáo

Tệp ZIP là gì?

Có thể bạn đã từng gặp và làm việc với các tệp ZIP. Vâng, những tệp có phần mở rộng tệp

>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
1 ở khắp mọi nơi. Các tệp ZIP, còn được gọi là kho lưu trữ ZIP, là các tệp sử dụng định dạng tệp ZIP

PKWARE là công ty đã tạo và triển khai định dạng tệp này lần đầu tiên. Công ty đã tổng hợp và duy trì đặc tả định dạng hiện tại, được cung cấp công khai và cho phép tạo ra các sản phẩm, chương trình và quy trình đọc và ghi tệp bằng định dạng tệp ZIP

Định dạng tệp ZIP là định dạng truyền và lưu trữ tệp đa nền tảng, có thể tương tác với nhau. Nó kết hợp nén dữ liệu không mất dữ liệu, quản lý tệp và mã hóa dữ liệu

Nén dữ liệu không phải là yêu cầu đối với tệp lưu trữ được coi là tệp ZIP. Vì vậy, bạn có thể có các tệp thành viên nén hoặc không nén trong kho lưu trữ ZIP của mình. Định dạng tệp ZIP hỗ trợ một số thuật toán nén, mặc dù Deflate là phổ biến nhất. Định dạng này cũng hỗ trợ kiểm tra tính toàn vẹn của thông tin với CRC32

Mặc dù có các định dạng lưu trữ tương tự khác, chẳng hạn như tệp RAR và TAR, định dạng tệp ZIP đã nhanh chóng trở thành tiêu chuẩn chung để lưu trữ dữ liệu hiệu quả và trao đổi dữ liệu qua mạng máy tính

Các tệp ZIP ở khắp mọi nơi. Ví dụ: các bộ ứng dụng văn phòng như Microsoft Office và Libre Office dựa vào định dạng tệp ZIP làm tệp chứa tài liệu của chúng. Điều này có nghĩa là các tệp

>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
2,
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
3,
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
4,
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
5,
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
6,
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
7 thực sự là các tệp lưu trữ ZIP chứa một số tệp và thư mục tạo nên mỗi tài liệu. Các tệp phổ biến khác sử dụng định dạng ZIP bao gồm các tệp
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
8,
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
9 và
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
70

Bạn có thể quen thuộc với GitHub, nơi cung cấp dịch vụ lưu trữ web để phát triển phần mềm và kiểm soát phiên bản bằng Git. GitHub sử dụng các tệp ZIP để đóng gói các dự án phần mềm khi bạn tải chúng xuống máy tính cục bộ của mình. Ví dụ: bạn có thể tải xuống các giải pháp bài tập cho Python Basics. Sách Giới thiệu thực tế về Python 3 trong tệp ZIP hoặc bạn có thể tải xuống bất kỳ dự án nào khác mà bạn chọn

Các tệp ZIP cho phép bạn tổng hợp, nén và mã hóa các tệp thành một vùng chứa di động và có thể tương tác được. Bạn có thể truyền trực tuyến các tệp ZIP, chia chúng thành các phân đoạn, làm cho chúng tự giải nén, v.v.

Tại sao nên sử dụng tệp ZIP?

Biết cách tạo, đọc, viết và giải nén các tệp ZIP có thể là một kỹ năng hữu ích cho các nhà phát triển và chuyên gia làm việc với máy tính và thông tin kỹ thuật số. Trong số các lợi ích khác, tệp ZIP cho phép bạn

  • Giảm kích thước tệp và yêu cầu lưu trữ của chúng mà không làm mất thông tin
  • Cải thiện tốc độ truyền qua mạng do giảm kích thước và truyền một tệp
  • Đóng gói một số tệp có liên quan lại với nhau thành một kho lưu trữ duy nhất để quản lý hiệu quả
  • Nhóm mã của bạn vào một kho lưu trữ duy nhất cho mục đích phân phối
  • Bảo mật dữ liệu của bạn bằng cách sử dụng mã hóa, đây là yêu cầu phổ biến hiện nay
  • Đảm bảo tính toàn vẹn của thông tin của bạn để tránh những thay đổi ngẫu nhiên và độc hại đối với dữ liệu của bạn

Các tính năng này làm cho các tệp ZIP trở thành một bổ sung hữu ích cho hộp công cụ Python của bạn nếu bạn đang tìm kiếm một cách linh hoạt, di động và đáng tin cậy để lưu trữ các tệp kỹ thuật số của mình

Python có thể thao tác với các tệp ZIP không?

Đúng. Python có một số công cụ cho phép bạn thao tác với các tệp ZIP. Một số công cụ này có sẵn trong thư viện chuẩn của Python. Chúng bao gồm các thư viện cấp thấp để nén và giải nén dữ liệu bằng các thuật toán nén cụ thể, chẳng hạn như

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
72,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
73, v.v.

Python cũng cung cấp một mô-đun cấp cao có tên là

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 được thiết kế đặc biệt để tạo, đọc, viết, trích xuất và liệt kê nội dung của tệp ZIP. Trong hướng dẫn này, bạn sẽ tìm hiểu về
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 của Python và cách sử dụng nó một cách hiệu quả

Thao tác với các tệp ZIP hiện có với
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 của Python

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 của Python cung cấp các lớp và hàm thuận tiện cho phép bạn tạo, đọc, viết, trích xuất và liệt kê nội dung của các tệp ZIP của mình. Dưới đây là một số tính năng bổ sung mà
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 hỗ trợ

  • Tệp ZIP lớn hơn 4 GiB [tệp ZIP64]
  • giải mã dữ liệu
  • Một số thuật toán nén, chẳng hạn như Deflate, Bzip2 và LZMA
  • Kiểm tra tính toàn vẹn thông tin với CRC32

Xin lưu ý rằng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 có một số hạn chế. Ví dụ: tính năng giải mã dữ liệu hiện tại có thể khá chậm vì nó sử dụng mã Python thuần túy. Mô-đun không thể xử lý việc tạo các tệp ZIP được mã hóa. Cuối cùng, việc sử dụng các tệp ZIP nhiều đĩa cũng không được hỗ trợ. Bất chấp những hạn chế này,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 vẫn là một công cụ tuyệt vời và hữu ích. Tiếp tục đọc để khám phá khả năng của nó

Loại bỏ các quảng cáo

Mở tệp ZIP để đọc và viết

Trong mô-đun

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3, bạn sẽ tìm thấy lớp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92. Lớp này hoạt động khá giống hàm
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
93 tích hợp sẵn của Python, cho phép bạn mở các tệp ZIP của mình bằng các chế độ khác nhau. Chế độ đọc [
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
94] là mặc định. Bạn cũng có thể sử dụng các chế độ ghi [
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
95], nối thêm [
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
96] và độc quyền [
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
97]. Bạn sẽ tìm hiểu thêm về từng điều này trong giây lát

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 triển khai giao thức quản lý bối cảnh để bạn có thể sử dụng lớp trong câu lệnh
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8. Tính năng này cho phép bạn nhanh chóng mở và làm việc với tệp ZIP mà không cần lo lắng về việc đóng tệp sau khi hoàn thành công việc của mình

Trước khi viết bất kỳ đoạn mã nào, hãy đảm bảo rằng bạn có một bản sao của các tệp và tệp lưu trữ mà bạn sẽ sử dụng

Nhận tài liệu. Nhấp vào đây để nhận bản sao của các tệp và kho lưu trữ mà bạn sẽ sử dụng để chạy các ví dụ trong hướng dẫn về tệp zip này

Để môi trường làm việc của bạn sẵn sàng, hãy đặt các tài nguyên đã tải xuống vào một thư mục có tên là

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
70 trong thư mục chính của bạn. Khi bạn có các tệp ở đúng nơi, hãy chuyển đến thư mục mới được tạo và kích hoạt phiên tương tác Python ở đó

Để khởi động, bạn sẽ bắt đầu bằng cách đọc tệp ZIP có tên là

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71. Để làm điều đó, bạn có thể sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 ở chế độ đọc

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
0

Đối số đầu tiên cho trình khởi tạo của

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 có thể là một chuỗi đại diện cho đường dẫn đến tệp ZIP mà bạn cần mở. Đối số này cũng có thể chấp nhận các đối tượng giống như tệp và giống như đường dẫn. Trong ví dụ này, bạn sử dụng đường dẫn dựa trên chuỗi

Đối số thứ hai của

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 là một chuỗi ký tự đơn thể hiện chế độ mà bạn sẽ sử dụng để mở tệp. Như bạn đã biết ở phần đầu của phần này,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 có thể chấp nhận bốn chế độ có thể, tùy thuộc vào nhu cầu của bạn. Đối số vị trí của ________ 376 mặc định là ________ 294, vì vậy bạn có thể loại bỏ nó nếu bạn chỉ muốn mở kho lưu trữ để đọc

Bên trong câu lệnh

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8, bạn gọi
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
79 trên
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
80. Biến
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
80 hiện giữ thể hiện của chính
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92. Chức năng này cung cấp một cách nhanh chóng để hiển thị nội dung của tệp ZIP bên dưới trên màn hình của bạn. Đầu ra của hàm có định dạng bảng thân thiện với người dùng với ba cột thông tin

  • >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    83
  • >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    84
  • >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    85

Nếu bạn muốn đảm bảo rằng bạn đang nhắm mục tiêu tệp ZIP hợp lệ trước khi cố mở tệp đó, thì bạn có thể bọc

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 trong câu lệnh
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
87 …
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
88 và bắt bất kỳ ngoại lệ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
89 nào

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8

Ví dụ đầu tiên mở thành công

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 mà không gây ra ngoại lệ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
89. Đó là bởi vì
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 có định dạng ZIP hợp lệ. Mặt khác, ví dụ thứ hai không thành công khi mở
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
803, vì tệp không phải là tệp ZIP hợp lệ

Để kiểm tra tệp ZIP hợp lệ, bạn cũng có thể sử dụng chức năng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
804

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
4

Trong các ví dụ này, bạn sử dụng câu điều kiện với

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
804 làm điều kiện. Hàm này nhận đối số
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
806 giữ đường dẫn đến tệp ZIP trong hệ thống tệp của bạn. Đối số này có thể chấp nhận các đối tượng dạng chuỗi, dạng tệp hoặc dạng đường dẫn. Hàm trả về
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
807 nếu
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
806 là tệp ZIP hợp lệ. Nếu không, nó sẽ trả về
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
809

Bây giờ, giả sử bạn muốn thêm

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810 vào kho lưu trữ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
811 bằng cách sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92. Để làm điều đó, bạn có thể sử dụng chế độ ghi [______295]. Chế độ này mở tệp ZIP để ghi. Nếu tệp ZIP mục tiêu tồn tại, thì chế độ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
95 sẽ cắt bớt tệp đó và ghi bất kỳ nội dung mới nào bạn chuyển vào

Ghi chú. Nếu bạn đang sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 với các tệp hiện có thì bạn nên cẩn thận với chế độ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
95. Bạn có thể cắt bớt tệp ZIP của mình và mất tất cả nội dung gốc

Nếu tệp ZIP mục tiêu không tồn tại, thì

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 sẽ tạo tệp đó cho bạn khi bạn đóng kho lưu trữ

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8

Sau khi chạy mã này, bạn sẽ có tệp

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
811 trong thư mục
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
70 của mình. Nếu bạn liệt kê nội dung tệp bằng cách sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
79, thì bạn sẽ nhận thấy rằng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810 sẽ ở đó. Trong ví dụ này, bạn gọi
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
822 trên đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92. Phương pháp này cho phép bạn ghi các tệp thành viên vào kho lưu trữ ZIP của mình. Lưu ý rằng đối số của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
822 phải là một tệp hiện có

Ghi chú.

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 đủ thông minh để tạo một kho lưu trữ mới khi bạn sử dụng lớp ở chế độ viết và kho lưu trữ đích không tồn tại. Tuy nhiên, lớp không tạo các thư mục mới trong đường dẫn đến tệp ZIP đích nếu các thư mục đó chưa tồn tại

Điều đó giải thích tại sao đoạn mã sau không hoạt động

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'

Bởi vì thư mục

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
826 trong đường dẫn đến tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
811 đích không tồn tại, nên bạn có một ngoại lệ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
828

Chế độ chắp thêm [

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
96] cho phép bạn nối các tệp thành viên mới vào tệp ZIP hiện có. Chế độ này không cắt bớt kho lưu trữ, vì vậy nội dung gốc của nó sẽ an toàn. Nếu tệp ZIP đích không tồn tại, thì chế độ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
96 sẽ tạo một tệp mới cho bạn rồi nối thêm bất kỳ tệp đầu vào nào mà bạn chuyển làm đối số cho
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
822

Để thử chế độ

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
96, hãy tiếp tục và thêm tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
833 vào kho lưu trữ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
811 mới tạo của bạn

>>>

>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13

Tại đây, bạn sử dụng chế độ chắp thêm để thêm

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
833 vào tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
811. Sau đó, bạn chạy
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
79 để xác nhận rằng tệp mới có trong tệp ZIP

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 cũng hỗ trợ chế độ độc quyền [______297]. Chế độ này cho phép bạn tạo riêng các tệp ZIP mới và ghi các tệp thành viên mới vào chúng. Bạn sẽ sử dụng chế độ độc quyền khi muốn tạo một tệp ZIP mới mà không ghi đè lên tệp hiện có. Nếu tệp mục tiêu đã tồn tại, thì bạn nhận được
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
840

Cuối cùng, nếu bạn tạo một tệp ZIP bằng cách sử dụng chế độ

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
95,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
96 hoặc
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
97 và sau đó đóng kho lưu trữ mà không thêm bất kỳ tệp thành viên nào, thì
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 sẽ tạo một kho lưu trữ trống với định dạng ZIP phù hợp

Loại bỏ các quảng cáo

Đọc siêu dữ liệu từ tệp ZIP

Bạn đã đưa

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
79 vào hoạt động. Đó là một phương pháp hữu ích mà bạn có thể sử dụng để liệt kê nội dung của các tệp ZIP của mình một cách nhanh chóng. Cùng với
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
79, lớp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 cung cấp một số phương thức tiện dụng để trích xuất siêu dữ liệu từ các tệp ZIP hiện có

Dưới đây là tóm tắt các phương pháp đó

MethodDescription

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
848Trả về một đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849 với thông tin về tệp thành viên được cung cấp bởi
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
806. Lưu ý rằng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
806 phải giữ đường dẫn đến tệp đích bên trong tệp ZIP bên dưới.
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
852Trả về danh sách các đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849, mỗi đối tượng một tệp thành viên.
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
854Trả về danh sách chứa tên của tất cả các tệp thành viên trong kho lưu trữ cơ bản. Tên trong danh sách này là đối số hợp lệ cho
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
855

Với ba công cụ này, bạn có thể truy xuất rất nhiều thông tin hữu ích về nội dung của các tệp ZIP của mình. Ví dụ: hãy xem ví dụ sau, sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
855

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
7

Như bạn đã học trong bảng trên,

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
855 lấy một tệp thành viên làm đối số và trả về một đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849 với thông tin về nó

Ghi chú.

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849 không có ý định khởi tạo trực tiếp. Các phương thức
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
855 và
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
852 tự động trả về các đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849 khi bạn gọi chúng. Tuy nhiên,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849 bao gồm một phương thức lớp có tên là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
864, cho phép bạn khởi tạo lớp một cách rõ ràng nếu bạn cần thực hiện

Các đối tượng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849 có một số thuộc tính cho phép bạn truy xuất thông tin có giá trị về tệp thành viên đích. Ví dụ:
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
866 và
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
867 lần lượt giữ kích thước tính bằng byte của tệp gốc và tệp nén. Lớp này cũng có một số thuộc tính hữu ích khác, chẳng hạn như
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
868 và
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
869, trả về tên tệp và ngày sửa đổi cuối cùng

Ghi chú. Theo mặc định,

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 không nén các tệp đầu vào để thêm chúng vào kho lưu trữ cuối cùng. Đó là lý do tại sao kích thước và kích thước nén giống nhau trong ví dụ trên. Bạn sẽ tìm hiểu thêm về chủ đề này trong phần Nén tệp và thư mục bên dưới

Với

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
852, bạn có thể trích xuất thông tin từ tất cả các tệp trong một kho lưu trữ nhất định. Đây là một ví dụ sử dụng phương pháp này để tạo một báo cáo tối thiểu với thông tin về tất cả các tệp thành viên trong kho lưu trữ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 của bạn

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
9

Vòng lặp

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
873 lặp lại các đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849 từ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
852, truy xuất tên tệp, ngày sửa đổi cuối cùng, kích thước bình thường và kích thước nén của từng tệp thành viên. Trong ví dụ này, bạn đã sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
876 để định dạng ngày theo cách mà con người có thể đọc được

Ghi chú. Ví dụ trên được chuyển thể từ zipfile — ZIP Archive Access

Nếu bạn chỉ cần thực hiện kiểm tra nhanh một tệp ZIP và liệt kê tên các tệp thành viên của nó, thì bạn có thể sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
854

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
7

Vì tên tệp trong đầu ra này là đối số hợp lệ của

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
855, nên bạn có thể kết hợp hai phương pháp này để chỉ truy xuất thông tin về các tệp thành viên được chọn

Ví dụ: bạn có thể có tệp ZIP chứa các loại tệp thành viên khác nhau [

>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
2,
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
3,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
881, v.v.]. Thay vì lấy thông tin đầy đủ với
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
852, bạn chỉ cần lấy thông tin về các tệp
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
2. Sau đó, bạn có thể lọc các tệp theo phần mở rộng của chúng và chỉ gọi
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
855 trên các tệp
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
2 của bạn. Đi trước và cung cấp cho nó một thử

Đọc từ và ghi vào tệp thành viên

Đôi khi bạn có một tệp ZIP và cần đọc nội dung của một tệp thành viên nhất định mà không cần giải nén nó. Để làm điều đó, bạn có thể sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
886. Phương thức này lấy một tệp thành viên là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
887 và trả về nội dung của tệp đó dưới dạng byte

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8

Để sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
886, bạn cần mở tệp ZIP để đọc hoặc nối thêm. Lưu ý rằng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
886 trả về nội dung của tệp đích dưới dạng luồng byte. Trong ví dụ này, bạn sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
890 để chia luồng thành các dòng, sử dụng ký tự nguồn cấp dữ liệu dòng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
891 làm dấu phân cách. Bởi vì
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
890 đang hoạt động trên một đối tượng byte, bạn cần thêm một
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
893 ở đầu vào chuỗi được sử dụng làm đối số

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
894 cũng chấp nhận đối số vị trí thứ hai có tên là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
895. Đối số này cho phép bạn cung cấp mật khẩu để đọc các tệp được mã hóa. Để thử tính năng này, bạn có thể dựa vào tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
896 mà bạn đã tải xuống cùng với tài liệu cho hướng dẫn này

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
80

Trong ví dụ đầu tiên, bạn cung cấp mật khẩu

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
897 để đọc tệp được mã hóa của mình. Đối số
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
895 chấp nhận các giá trị của loại byte. Nếu bạn sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
886 trên một tệp được mã hóa mà không cung cấp mật khẩu được yêu cầu, thì bạn sẽ nhận được một
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
400, như bạn có thể lưu ý trong ví dụ thứ hai

Ghi chú.

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 của Python hỗ trợ giải mã. Tuy nhiên, nó không hỗ trợ tạo các tệp ZIP được mã hóa. Đó là lý do tại sao bạn cần sử dụng trình lưu trữ tệp bên ngoài để mã hóa tệp của mình

Một số trình lưu trữ tệp phổ biến bao gồm 7z và WinRAR cho Windows, Trình quản lý lưu trữ Ark và GNOME cho Linux và Archiver cho macOS

Đối với các tệp ZIP được mã hóa lớn, hãy nhớ rằng thao tác giải mã có thể cực kỳ chậm vì nó được triển khai bằng Python thuần túy. Trong những trường hợp như vậy, hãy cân nhắc sử dụng một chương trình chuyên biệt để xử lý các tệp lưu trữ của bạn thay vì sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3

Nếu bạn thường xuyên làm việc với các tệp được mã hóa, thì bạn có thể muốn tránh cung cấp mật khẩu giải mã mỗi khi bạn gọi

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
886 hoặc một phương thức khác chấp nhận đối số
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
895. Nếu đúng như vậy, bạn có thể sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
405 để đặt mật khẩu chung

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
81

Với

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
406, bạn chỉ cần cung cấp mật khẩu một lần.
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 sử dụng mật khẩu duy nhất đó để giải mã tất cả các tệp thành viên

Ngược lại, nếu bạn có các tệp ZIP có mật khẩu khác nhau cho các tệp thành viên riêng lẻ, thì bạn cần cung cấp mật khẩu cụ thể cho từng tệp bằng cách sử dụng đối số

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
895 của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
886

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
82

Trong ví dụ này, bạn sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
410 làm mật khẩu để đọc
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810 và
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
412 để đọc
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
413. Chi tiết cuối cùng cần xem xét là khi bạn sử dụng đối số
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
895, bạn đang ghi đè bất kỳ mật khẩu cấp lưu trữ nào mà bạn có thể đã đặt với
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
406

Ghi chú. Gọi

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
886 trên tệp ZIP sử dụng phương pháp nén không được hỗ trợ sẽ làm tăng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
417. Bạn cũng gặp lỗi nếu mô-đun nén được yêu cầu không có sẵn trong bản cài đặt Python của bạn

Nếu bạn đang tìm kiếm một cách linh hoạt hơn để đọc từ các tệp thành viên cũng như tạo và thêm các tệp thành viên mới vào kho lưu trữ, thì

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
418 là dành cho bạn. Giống như hàm
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
93 tích hợp sẵn, phương thức này triển khai giao thức quản lý bối cảnh và do đó nó hỗ trợ câu lệnh
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
83

Trong ví dụ này, bạn mở

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810 để đọc. Đối số đầu tiên của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
422 là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
887, cho biết tệp thành viên mà bạn muốn mở. Đối số thứ hai là chế độ, mặc định là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
94 như bình thường.
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
418 cũng chấp nhận đối số
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
895 để mở tệp được mã hóa. Đối số này hoạt động giống như đối số
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
895 tương đương trong
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
886

Bạn cũng có thể sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
422 với chế độ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
95. Chế độ này cho phép bạn tạo một tệp thành viên mới, ghi nội dung vào đó và cuối cùng nối tệp vào kho lưu trữ bên dưới mà bạn nên mở ở chế độ nối thêm

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
84

Trong đoạn mã đầu tiên, bạn mở

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 ở chế độ nối thêm [
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
96]. Sau đó, bạn tạo
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
833 bằng cách gọi
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
422 với chế độ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
95. Hàm này trả về một đối tượng giống như tệp hỗ trợ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
822, cho phép bạn ghi byte vào tệp mới tạo

Ghi chú. Bạn cần cung cấp tên tệp không tồn tại cho

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
422. Nếu bạn sử dụng tên tệp đã tồn tại trong kho lưu trữ cơ bản thì bạn sẽ nhận được một tệp trùng lặp và một ngoại lệ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
438

Trong ví dụ này, bạn viết

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
439 thành
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
833. Khi luồng thực thi thoát khỏi câu lệnh bên trong
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8, Python ghi các byte đầu vào vào tệp thành viên. Khi câu lệnh
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8 bên ngoài thoát, Python ghi
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
833 vào tệp ZIP bên dưới,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71

Đoạn mã thứ hai xác nhận rằng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
833 hiện là tệp thành viên của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71. Một chi tiết cần lưu ý trong đầu ra của ví dụ này là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
822 đặt ngày
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
84 của tệp mới được thêm vào thành
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
449, đây là một hành vi kỳ lạ mà bạn nên ghi nhớ khi sử dụng phương pháp này

Loại bỏ các quảng cáo

Đọc nội dung của tệp thành viên dưới dạng văn bản

Như bạn đã học trong phần trên, bạn có thể sử dụng các phương pháp

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
886 và
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
822 để đọc và ghi vào các tệp thành viên mà không cần giải nén chúng từ kho lưu trữ ZIP chứa. Cả hai phương thức này đều hoạt động riêng với byte

Tuy nhiên, khi bạn có một kho lưu trữ ZIP chứa các tệp văn bản, bạn có thể muốn đọc nội dung của chúng dưới dạng văn bản thay vì dưới dạng byte. Có ít nhất hai cách để làm điều này. Bạn có thể dùng

  1. >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    452
  2. >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    453

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
894 trả về nội dung của tệp thành viên đích dưới dạng byte, nên
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
455 có thể hoạt động trực tiếp trên các byte này. Phương thức
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
455 giải mã một đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
457 thành một chuỗi sử dụng định dạng mã hóa ký tự đã cho

Đây là cách bạn có thể sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
455 để đọc văn bản từ tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810 trong kho lưu trữ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 của mình

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
85

Trong ví dụ này, bạn đọc nội dung của

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810 dưới dạng byte. Sau đó, bạn gọi
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
455 để giải mã các byte thành một chuỗi bằng mã hóa UTF-8. Để đặt đối số
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
463, bạn sử dụng chuỗi
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
464. Tuy nhiên, bạn có thể sử dụng bất kỳ mã hóa hợp lệ nào khác, chẳng hạn như UTF-16 hoặc cp1252, có thể được biểu thị dưới dạng chuỗi phân biệt chữ hoa chữ thường. Lưu ý rằng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
464 là giá trị mặc định của đối số
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
463 thành
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
455

Điều quan trọng cần lưu ý là bạn cần biết trước định dạng mã hóa ký tự của bất kỳ tệp thành viên nào mà bạn muốn xử lý bằng cách sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
455. Nếu bạn sử dụng mã hóa ký tự sai, thì mã của bạn sẽ không giải mã chính xác các byte bên dưới thành văn bản và bạn có thể nhận được rất nhiều ký tự không thể giải mã được

Tùy chọn thứ hai để đọc văn bản từ tệp thành viên là sử dụng đối tượng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
453, cung cấp luồng văn bản được đệm. Lần này bạn cần sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
422 thay vì
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
886. Đây là một ví dụ về việc sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
453 để đọc nội dung của tệp thành viên
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810 dưới dạng luồng văn bản

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
86

Trong câu lệnh bên trong

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8 trong ví dụ này, bạn mở tệp thành viên
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810 từ kho lưu trữ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 của mình. Sau đó, bạn chuyển đối tượng giống như tệp nhị phân kết quả,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
477, làm đối số cho
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
453. Thao tác này tạo luồng văn bản được đệm bằng cách giải mã nội dung của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
477 bằng định dạng mã hóa ký tự UTF-8. Kết quả là bạn nhận được một luồng văn bản trực tiếp từ tệp thành viên mục tiêu của mình

Cũng giống như với

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
480, lớp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
453 nhận đối số
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
463. Bạn phải luôn chỉ định một giá trị cho đối số này vì mã hóa văn bản mặc định tùy thuộc vào hệ thống đang chạy mã và có thể không phải là giá trị phù hợp cho tệp mà bạn đang cố giải mã

Trích xuất tệp thành viên từ kho lưu trữ ZIP của bạn

Trích xuất nội dung của một kho lưu trữ nhất định là một trong những thao tác phổ biến nhất mà bạn sẽ thực hiện trên các tệp ZIP. Tùy thuộc vào nhu cầu của bạn, bạn có thể muốn giải nén một tệp tại một thời điểm hoặc tất cả các tệp trong một lần

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
483 cho phép bạn hoàn thành nhiệm vụ đầu tiên. Phương pháp này lấy tên của tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
484 và trích xuất nó vào một thư mục nhất định được ký hiệu bởi
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
485. Đích
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
485 mặc định cho thư mục hiện tại

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
87

Bây giờ

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
833 sẽ nằm trong thư mục
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
488 của bạn. Nếu tên tệp mục tiêu đã tồn tại trong thư mục đầu ra, thì
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
489 sẽ ghi đè lên nó mà không yêu cầu xác nhận. Nếu thư mục đầu ra không tồn tại, thì
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
489 sẽ tạo nó cho bạn. Lưu ý rằng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
489 trả về đường dẫn đến tệp được giải nén

Tên của tệp thành viên phải là tên đầy đủ của tệp như được trả về bởi

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
854. Nó cũng có thể là một đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849 chứa thông tin của tệp

Bạn cũng có thể sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
489 với các tệp được mã hóa. Trong trường hợp đó, bạn cần cung cấp đối số
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
895 được yêu cầu hoặc đặt mật khẩu cấp lưu trữ bằng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
406

Khi trích xuất tất cả các tệp thành viên từ kho lưu trữ, bạn có thể sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
497. Đúng như tên gọi của nó, phương thức này trích xuất tất cả các tệp thành viên vào một đường dẫn đích, mặc định là thư mục hiện tại

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
88

Sau khi chạy mã này, tất cả nội dung hiện tại của

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 sẽ nằm trong thư mục
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
488 của bạn. Nếu bạn chuyển một thư mục không tồn tại cho
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
497, thì phương thức này sẽ tự động tạo thư mục. Cuối cùng, nếu bất kỳ tệp thành viên nào đã tồn tại trong thư mục đích, thì
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
497 sẽ ghi đè lên chúng mà không yêu cầu bạn xác nhận, vì vậy hãy cẩn thận

Nếu bạn chỉ cần trích xuất một số tệp thành viên từ một kho lưu trữ nhất định, thì bạn có thể sử dụng đối số

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
802. Đối số này chấp nhận một danh sách các tệp thành viên, đây sẽ là một tập hợp con của toàn bộ danh sách các tệp trong kho lưu trữ hiện tại. Cuối cùng, giống như
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
489, phương thức
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
497 cũng chấp nhận đối số
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
895 để trích xuất các tệp được mã hóa

Loại bỏ các quảng cáo

Đóng tệp ZIP sau khi sử dụng

Đôi khi, thật thuận tiện cho bạn khi mở một tệp ZIP nhất định mà không cần sử dụng câu lệnh

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8. Trong những trường hợp đó, bạn cần đóng kho lưu trữ theo cách thủ công sau khi sử dụng để hoàn thành mọi thao tác ghi và giải phóng các tài nguyên đã mua

Để làm điều đó, bạn có thể gọi

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
807 trên đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 của mình

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
89

Cuộc gọi đến

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
807 đóng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
80 cho bạn. Bạn phải gọi
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
807 trước khi thoát khỏi chương trình của mình. Nếu không, một số thao tác ghi có thể không được thực hiện. Ví dụ: nếu bạn mở tệp ZIP để nối thêm [_______296] tệp thành viên mới, thì bạn cần đóng kho lưu trữ để ghi tệp

Tạo, phổ biến và giải nén các tệp ZIP của riêng bạn

Cho đến nay, bạn đã học cách làm việc với các tệp ZIP hiện có. Bạn đã học cách đọc, viết và nối các tệp thành viên với chúng bằng cách sử dụng các chế độ khác nhau của

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92. Bạn cũng đã học cách đọc siêu dữ liệu có liên quan và cách trích xuất nội dung của một tệp ZIP nhất định

Trong phần này, bạn sẽ mã hóa một số ví dụ thực tế giúp bạn tìm hiểu cách tạo tệp ZIP từ một số tệp đầu vào và từ toàn bộ thư mục bằng cách sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 và các công cụ Python khác. Bạn cũng sẽ học cách sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 để nén tệp và hơn thế nữa

Tạo tệp ZIP từ nhiều tệp thông thường

Đôi khi bạn cần tạo một kho lưu trữ ZIP từ một số tệp liên quan. Bằng cách này, bạn có thể có tất cả các tệp trong một vùng chứa để phân phối chúng qua mạng máy tính hoặc chia sẻ chúng với bạn bè hoặc đồng nghiệp. Để làm được điều này, bạn có thể tạo danh sách các tệp mục tiêu và ghi chúng vào kho lưu trữ bằng cách sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 và một vòng lặp

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
40

Tại đây, bạn tạo một đối tượng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 với tên lưu trữ mong muốn làm đối số đầu tiên. Chế độ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
95 cho phép bạn ghi các tệp thành viên vào tệp ZIP cuối cùng

Vòng lặp

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
873 lặp lại danh sách các tệp đầu vào của bạn và ghi chúng vào tệp ZIP bên dưới bằng cách sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
822. Khi luồng thực thi thoát khỏi câu lệnh
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 sẽ tự động đóng kho lưu trữ, lưu các thay đổi cho bạn. Bây giờ bạn có một kho lưu trữ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
823 chứa tất cả các tệp từ danh sách tệp ban đầu của bạn

Xây dựng một tệp ZIP từ một thư mục

Gói nội dung của một thư mục vào một kho lưu trữ duy nhất là một trường hợp sử dụng hàng ngày khác cho các tệp ZIP. Python có một số công cụ mà bạn có thể sử dụng với

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 để tiếp cận tác vụ này. Ví dụ: bạn có thể sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
9 để đọc nội dung của một thư mục nhất định. Với thông tin đó, bạn có thể tạo kho lưu trữ vùng chứa bằng cách sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92

Trong thư mục

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
70, bạn có một thư mục con tên là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
828, với nội dung như sau

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
41

Trong

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
828, bạn chỉ có ba tệp thông thường. Bởi vì thư mục không chứa thư mục con, bạn có thể sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
830 để lặp trực tiếp nội dung của nó. Với ý tưởng này, đây là cách bạn có thể tạo một tệp ZIP từ nội dung của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
828

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
42

Trong ví dụ này, bạn tạo một đối tượng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
832 từ thư mục nguồn của mình. Câu lệnh
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8 đầu tiên tạo một đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 sẵn sàng để viết. Sau đó, lệnh gọi tới
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
835 trả về một trình vòng lặp trên các mục trong thư mục bên dưới

Bởi vì bạn không có bất kỳ thư mục con nào trong

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
828, hàm
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
835 chỉ mang lại các tệp. Vòng lặp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
873 lặp lại các tệp và ghi chúng vào kho lưu trữ

Trong trường hợp này, bạn chuyển

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
839 cho đối số thứ hai của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
822. Đối số này được gọi là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
841 và giữ tên của tệp thành viên bên trong kho lưu trữ kết quả. Tất cả các ví dụ mà bạn đã thấy cho đến nay đều dựa vào giá trị mặc định của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
841, chính là tên tệp mà bạn chuyển làm đối số đầu tiên cho
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
822

Nếu bạn không chuyển

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
839 thành
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
841, thì thư mục nguồn của bạn sẽ nằm ở thư mục gốc của tệp ZIP, đây cũng có thể là kết quả hợp lệ tùy thuộc vào nhu cầu của bạn

Bây giờ hãy kiểm tra thư mục

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
846 trong thư mục làm việc của bạn. Trong trường hợp này, bạn sẽ tìm thấy cấu trúc sau

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
43

Bạn có các tệp thông thường và một thư mục con chứa một tệp duy nhất trong đó. Nếu bạn muốn tạo một tệp ZIP có cùng cấu trúc bên trong này, thì bạn cần một công cụ lặp lại đệ quy qua cây thư mục bên dưới

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
846

Đây là cách nén một cây thư mục hoàn chỉnh, giống như ở trên, sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 cùng với
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849 từ mô-đun
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
9

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
44

Trong ví dụ này, bạn sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849 để duyệt đệ quy cây thư mục bên dưới
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
846. Sau đó, bạn ghi mọi tệp và thư mục con vào kho lưu trữ ZIP đích

Lần này, bạn sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
853 để lấy đường dẫn tương đối đến từng tệp và sau đó chuyển kết quả cho đối số thứ hai của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
822. Bằng cách này, tệp ZIP kết quả sẽ có cùng cấu trúc bên trong với thư mục nguồn ban đầu của bạn. Một lần nữa, bạn có thể loại bỏ đối số này nếu bạn muốn thư mục nguồn của mình nằm ở thư mục gốc của tệp ZIP của bạn

Loại bỏ các quảng cáo

Nén tập tin và thư mục

Nếu các tệp của bạn chiếm quá nhiều dung lượng ổ đĩa, thì bạn có thể xem xét việc nén chúng.

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 của Python hỗ trợ một số phương pháp nén phổ biến. Tuy nhiên, mô-đun không nén các tệp của bạn theo mặc định. Nếu bạn muốn làm cho các tệp của mình nhỏ hơn, thì bạn cần cung cấp rõ ràng một phương thức nén cho
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92

Thông thường, bạn sẽ sử dụng thuật ngữ được lưu trữ để chỉ các tệp thành viên được ghi vào tệp ZIP mà không cần nén. Đó là lý do tại sao phương pháp nén mặc định của

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 được gọi là ZIP_STORED, thực ra đề cập đến các tệp thành viên không nén được lưu trữ đơn giản trong kho lưu trữ chứa

Phương thức

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
858 là đối số thứ ba cho trình khởi tạo của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92. Nếu bạn muốn nén các tệp của mình trong khi ghi chúng vào kho lưu trữ ZIP, thì bạn có thể đặt đối số này thành một trong các hằng số sau

Phương pháp nén liên tục Mô-đun bắt buộc

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
860Deflate
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
862Bzip2
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
72
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
864LZMA
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
73

Đây là những phương pháp nén mà bạn hiện có thể sử dụng với

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92. Một phương pháp khác sẽ tăng một
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
417. Không có phương pháp nén bổ sung nào có sẵn cho
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 kể từ Python 3. 10

Như một yêu cầu bổ sung, nếu bạn chọn một trong các phương pháp này, thì mô-đun nén hỗ trợ nó phải có sẵn trong bản cài đặt Python của bạn. Nếu không, bạn sẽ nhận được một ngoại lệ

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
400 và mã của bạn sẽ bị hỏng

Một đối số có liên quan khác của

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 khi nói đến việc nén các tệp của bạn là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
871. Đối số này kiểm soát mức độ nén mà bạn sử dụng

Với phương thức Deflate,

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
871 có thể lấy các số nguyên từ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
873 đến
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
874. Với phương thức Bzip2, bạn có thể chuyển các số nguyên từ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
875 đến
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
874. Trong cả hai trường hợp, khi mức nén tăng lên, bạn sẽ nhận được mức nén cao hơn và tốc độ nén thấp hơn

Ghi chú. Các tệp nhị phân, chẳng hạn như PNG, JPG, MP3, v.v., đã sử dụng một số loại nén. Do đó, việc thêm chúng vào tệp ZIP có thể không làm cho dữ liệu nhỏ hơn vì nó đã được nén ở một mức độ nào đó.

Bây giờ, giả sử bạn muốn lưu trữ và nén nội dung của một thư mục nhất định bằng phương pháp Deflate, đây là phương pháp được sử dụng phổ biến nhất trong các tệp ZIP. Để làm điều đó, bạn có thể chạy đoạn mã sau

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
45

Trong ví dụ này, bạn chuyển

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
874 đến
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
871 để đạt được mức nén tối đa. Để cung cấp đối số này, bạn sử dụng đối số từ khóa. Bạn cần làm điều này vì
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
871 không phải là đối số vị trí thứ tư cho trình khởi tạo
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92

Ghi chú. Trình khởi tạo của

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 nhận đối số thứ tư có tên là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
882. Đó là một đối số Boolean yêu cầu
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 tạo các tệp ZIP có phần mở rộng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
884 cho các tệp lớn hơn 4 GB

Sau khi chạy mã này, bạn sẽ có tệp

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
885 trong thư mục hiện tại của mình. Nếu bạn so sánh kích thước của tệp đó với kích thước của tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 ban đầu, thì bạn sẽ nhận thấy kích thước giảm đáng kể

Tạo tệp ZIP tuần tự

Tạo các tệp ZIP theo trình tự có thể là một yêu cầu phổ biến khác trong lập trình hàng ngày của bạn. Ví dụ: bạn có thể cần tạo một tệp ZIP ban đầu có hoặc không có nội dung, sau đó nối thêm các tệp thành viên mới ngay khi chúng có sẵn. Trong tình huống này, bạn cần mở và đóng tệp ZIP mục tiêu nhiều lần

Để giải quyết vấn đề này, bạn có thể sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 ở chế độ chắp thêm [
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
96], như bạn đã làm. Chế độ này cho phép bạn thêm các tệp thành viên mới vào kho lưu trữ ZIP một cách an toàn mà không cắt bớt nội dung hiện tại của nó

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
46

Trong ví dụ này,

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
889 là hàm nối thêm tệp [
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
484] vào kho lưu trữ ZIP đầu vào [
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
891]. Để thực hiện hành động này, hàm sẽ mở và đóng kho lưu trữ đích mỗi khi bạn gọi nó. Sử dụng một hàm để thực hiện tác vụ này cho phép bạn sử dụng lại mã bao nhiêu lần tùy ý

Hàm

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
892 là hàm tạo mô phỏng luồng tệp để xử lý. Trong khi đó, vòng lặp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
873 liên tục thêm các tệp thành viên vào
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
894 bằng cách sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
895. Nếu bạn kiểm tra thư mục làm việc của mình sau khi chạy mã này, thì bạn sẽ tìm thấy một kho lưu trữ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
894 chứa ba tệp mà bạn đã chuyển vào vòng lặp

Loại bỏ các quảng cáo

Giải nén tập tin và thư mục

Một trong những thao tác phổ biến nhất mà bạn từng thực hiện trên các tệp ZIP là trích xuất nội dung của chúng vào một thư mục nhất định trong hệ thống tệp của bạn. Bạn đã học những kiến ​​thức cơ bản về cách sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
489 và
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
497 để giải nén một hoặc tất cả tệp từ kho lưu trữ

Như một ví dụ bổ sung, hãy quay lại tệp

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 của bạn. Tại thời điểm này, kho lưu trữ chứa bốn tệp thuộc các loại khác nhau. Bạn có hai tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
881 và hai tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
01. Bây giờ giả sử bạn chỉ muốn trích xuất các tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
01. Để làm như vậy, bạn có thể chạy đoạn mã sau

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
47

Câu lệnh

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
8 mở
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 để đọc. Vòng lặp lặp qua từng tệp trong kho lưu trữ bằng cách sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
05, trong khi câu lệnh điều kiện kiểm tra xem tên tệp có kết thúc bằng phần mở rộng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
01 không. Nếu đúng như vậy, thì bạn giải nén tệp hiện có vào thư mục đích,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
488, sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
489

Khám phá các lớp học bổ sung từ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3

Cho đến nay, bạn đã học về

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 và
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
849, đây là hai trong số các lớp có sẵn trong
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3. Mô-đun này cũng cung cấp thêm hai lớp có thể hữu ích trong một số trường hợp. Các lớp đó là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
13 và
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
14. Trong hai phần sau, bạn sẽ tìm hiểu kiến ​​thức cơ bản về các lớp này và các tính năng chính của chúng

Tìm
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
15 trong tệp ZIP

Khi bạn mở tệp ZIP bằng ứng dụng trình lưu trữ yêu thích của mình, bạn sẽ thấy cấu trúc bên trong của tệp lưu trữ. Bạn có thể có các tệp ở thư mục gốc của kho lưu trữ. Bạn cũng có thể có các thư mục con chứa nhiều tệp hơn. Kho lưu trữ trông giống như một thư mục bình thường trên hệ thống tệp của bạn, với mỗi tệp nằm ở một đường dẫn cụ thể

Lớp

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
13 cho phép bạn xây dựng các đối tượng đường dẫn để nhanh chóng tạo và quản lý đường dẫn đến các tệp và thư mục thành viên bên trong một tệp ZIP nhất định. Lớp có hai đối số

  • >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    17 chấp nhận tệp ZIP, dưới dạng đối tượng
    >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    92 hoặc đường dẫn dựa trên chuỗi tới tệp ZIP vật lý
  • >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    19 giữ vị trí của một tệp hoặc thư mục thành viên cụ thể bên trong kho lưu trữ. Nó mặc định là chuỗi rỗng, đại diện cho thư mục gốc của kho lưu trữ

Với người bạn cũ của bạn

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 làm mục tiêu, hãy chạy đoạn mã sau

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
48

Mã này cho thấy rằng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
13 thực hiện một số tính năng phổ biến đối với đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
832. Bạn có thể lấy tên của tệp bằng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
23. Bạn có thể kiểm tra xem đường dẫn có trỏ đến một tệp thông thường với
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
24 không. Bạn có thể kiểm tra xem một tệp nhất định có tồn tại bên trong một tệp ZIP cụ thể hay không và hơn thế nữa

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
15 cũng cung cấp phương thức
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
422 để mở tệp thành viên bằng các chế độ khác nhau. Ví dụ: mã bên dưới mở
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810 để đọc

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
49

Với

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
15, bạn có thể nhanh chóng tạo một đối tượng đường dẫn trỏ đến một tệp thành viên cụ thể trong một tệp ZIP nhất định và truy cập nội dung của nó ngay lập tức bằng cách sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
422

Giống như với đối tượng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
832, bạn có thể liệt kê nội dung của tệp ZIP bằng cách gọi
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
835 trên đối tượng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
13

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
80

Rõ ràng là

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
13 cung cấp nhiều tính năng hữu ích mà bạn có thể sử dụng để quản lý các tệp thành viên trong kho lưu trữ ZIP của mình gần như ngay lập tức

Loại bỏ các quảng cáo

Xây dựng các tệp ZIP có thể nhập bằng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
34

Một lớp hữu ích khác trong

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
34. Lớp này khá giống với lớp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 và nó đặc biệt hữu ích khi bạn cần gói các mô-đun và gói Python vào các tệp ZIP. Sự khác biệt chính so với
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92 là trình khởi tạo của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
34 nhận một đối số tùy chọn có tên là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
40, cho phép bạn tối ưu hóa mã Python bằng cách biên dịch nó thành mã byte trước khi lưu trữ.

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
34 cung cấp giao diện giống như
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
92, với việc bổ sung
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
43. Phương thức này có thể lấy một tệp Python [
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
44] làm đối số và thêm nó vào tệp ZIP bên dưới. Nếu
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
40 là
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
46 [mặc định], thì tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
44 sẽ tự động được biên dịch thành tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
48 và sau đó được thêm vào kho lưu trữ đích. Lý do tại sao điều này xảy ra?

Kể từ phiên bản 2. 3, trình thông dịch Python đã hỗ trợ nhập mã Python từ các tệp ZIP, một khả năng được gọi là nhập Zip. Tính năng này khá tiện lợi. Nó cho phép bạn tạo các tệp ZIP có thể nhập để phân phối các mô-đun và gói của bạn dưới dạng một kho lưu trữ duy nhất

Ghi chú. Bạn cũng có thể sử dụng định dạng tệp ZIP để tạo và phân phối các ứng dụng thực thi Python, thường được gọi là ứng dụng Python Zip. Để tìm hiểu cách tạo chúng, hãy xem Python zipapp. Xây dựng các ứng dụng Zip có thể thực thi

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
34 rất hữu ích khi bạn cần tạo các tệp ZIP có thể nhập. Đóng gói tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
48 thay vì tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
44 giúp quá trình nhập hiệu quả hơn vì nó bỏ qua bước biên dịch

Bên trong thư mục

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
70, bạn có module
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
53 với nội dung như sau

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
81

Đoạn mã này định nghĩa một hàm có tên là

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
54, lấy ____5887 làm đối số và in thông báo lời chào ra màn hình. Bây giờ, giả sử bạn muốn đóng gói mô-đun này thành tệp ZIP cho mục đích phân phối. Để làm điều đó, bạn có thể chạy đoạn mã sau

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
82

Trong ví dụ này, cuộc gọi đến

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
43 sẽ tự động biên dịch
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
53 thành
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
58 và lưu trữ nó trong
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
811. Điều này trở nên rõ ràng khi bạn liệt kê nội dung của kho lưu trữ bằng cách sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
79

Khi bạn đã đóng gói

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
53 vào tệp ZIP, thì bạn có thể sử dụng hệ thống nhập của Python để nhập mô-đun này từ kho lưu trữ chứa nó

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
83

Bước đầu tiên để nhập mã từ tệp ZIP là cung cấp tệp đó trong

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
62. Biến này chứa một danh sách các chuỗi chỉ định đường dẫn tìm kiếm của Python cho các mô-đun. Để thêm một mục mới vào
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
62, bạn có thể sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
64

Để ví dụ này hoạt động, bạn cần thay đổi đường dẫn giữ chỗ và chuyển đường dẫn tới

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
811 trên hệ thống tệp của bạn. Sau khi tệp ZIP có thể nhập của bạn nằm trong danh sách này, thì bạn có thể nhập mã của mình giống như bạn làm với một mô-đun thông thường

Cuối cùng, hãy xem xét thư mục con

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
66 trong thư mục làm việc của bạn. Nó chứa một gói Python nhỏ với cấu trúc sau

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
84

Mô-đun

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
67 biến thư mục
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
66 thành gói Python. Mô-đun
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
53 giống với mô-đun mà bạn đã sử dụng trong ví dụ trên. Bây giờ, giả sử bạn muốn gói gói này thành một tệp ZIP. Nếu đúng như vậy, thì bạn có thể làm như sau

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
85

Lệnh gọi tới

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
43 lấy gói
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
477 làm đối số, tìm kiếm các tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
44 bên trong gói đó, biên dịch chúng thành tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
48 và cuối cùng thêm chúng vào tệp ZIP đích,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
811. Một lần nữa, bạn có thể nhập mã của mình từ kho lưu trữ này bằng cách làm theo các bước mà bạn đã học trước đó

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
86

Vì mã của bạn hiện đang ở trong một gói nên trước tiên bạn cần nhập mô-đun

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
477 từ gói
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
477. Sau đó, bạn có thể truy cập chức năng
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
54 của mình một cách bình thường

Loại bỏ các quảng cáo

Chạy
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 từ dòng lệnh của bạn

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 của Python cũng cung cấp giao diện dòng lệnh tối thiểu cho phép bạn nhanh chóng truy cập chức năng chính của mô-đun. Ví dụ: bạn có thể sử dụng tùy chọn
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
80 hoặc
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
81 để liệt kê nội dung của tệp ZIP hiện có

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
87

Lệnh này hiển thị cùng một đầu ra như một lệnh gọi tương đương tới

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
79 trên kho lưu trữ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71

Bây giờ, giả sử bạn muốn tạo một tệp ZIP mới chứa một số tệp đầu vào. Trong trường hợp đó, bạn có thể sử dụng tùy chọn

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
84 hoặc
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
85

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
88

Lệnh này tạo một tệp

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
86 chứa các tệp
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
413,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
89 của bạn

Nếu bạn cần tạo một tệp ZIP để lưu trữ toàn bộ thư mục thì sao? . Bạn có thể tạo tệp ZIP từ thư mục đó bằng cách sử dụng lệnh sau

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
89

Với lệnh này,

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 đặt
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
828 vào thư mục gốc của tệp kết quả
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
93. Như thường lệ, bạn có thể liệt kê nội dung lưu trữ bằng cách chạy
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 với tùy chọn
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
80

Ghi chú. Khi bạn sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 để tạo tệp lưu trữ từ dòng lệnh của mình, thư viện sẽ ngầm sử dụng thuật toán nén Deflate khi lưu trữ tệp của bạn

Bạn cũng có thể trích xuất tất cả nội dung của một tệp ZIP nhất định bằng cách sử dụng tùy chọn

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
97 hoặc
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
98 từ dòng lệnh của mình

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
0

Sau khi chạy lệnh này, bạn sẽ có một thư mục

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
99 mới trong thư mục làm việc của mình. Thư mục mới sẽ chứa các tệp hiện tại trong kho lưu trữ
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 của bạn

Tùy chọn cuối cùng mà bạn có thể sử dụng với

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 từ dòng lệnh là
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
02 hoặc
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
03. Tùy chọn này cho phép bạn kiểm tra xem một tệp đã cho có phải là tệp ZIP hợp lệ hay không. Đi trước và cung cấp cho nó một thử

Sử dụng các thư viện khác để quản lý tệp ZIP

Có một vài công cụ khác trong thư viện chuẩn Python mà bạn có thể sử dụng để lưu trữ, nén và giải nén các tệp của mình ở mức thấp hơn.

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 của Python sử dụng một số trong số này trong nội bộ, chủ yếu cho mục đích nén. Dưới đây là tóm tắt về một số công cụ này

Mô tả mô-đun

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71Cho phép nén và giải nén bằng thư viện zlib
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
72Cung cấp giao diện để nén và giải nén dữ liệu bằng thuật toán nén Bzip2
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
73Cung cấp các lớp và chức năng để nén và giải nén dữ liệu bằng thuật toán nén LZMA

Không giống như

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3, một số mô-đun này cho phép bạn nén và giải nén dữ liệu từ bộ nhớ và luồng dữ liệu ngoài các tệp và kho lưu trữ thông thường

Trong thư viện chuẩn của Python, bạn cũng sẽ tìm thấy

>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
09, hỗ trợ định dạng lưu trữ TAR. Ngoài ra còn có một mô-đun có tên là
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
10, cung cấp giao diện để nén và giải nén dữ liệu, tương tự như cách chương trình GNU Gzip thực hiện

Ví dụ: bạn có thể sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
10 để tạo tệp nén chứa một số văn bản

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
1

Khi bạn chạy mã này, bạn sẽ có một kho lưu trữ

>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
12 chứa phiên bản nén của
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810 trong thư mục hiện tại của bạn. Bên trong
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
810, bạn sẽ tìm thấy văn bản
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
15

Một cách nhanh chóng và cấp cao để tạo tệp ZIP mà không cần sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 là sử dụng
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
17. Mô-đun này cho phép bạn thực hiện một số thao tác cấp cao trên tệp và bộ sưu tập tệp. Khi nói đến hoạt động lưu trữ, bạn có
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
18, có thể tạo tệp lưu trữ, chẳng hạn như tệp ZIP hoặc TAR

>>>

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
2

Mã này tạo một tệp nén có tên là

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
71 trong thư mục làm việc của bạn. Tệp ZIP này sẽ chứa tất cả các tệp trong thư mục đầu vào,
>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
828. Hàm
>>> import zipfile

>>> with zipfile.ZipFile["hello.zip", mode="a"] as archive:
..     archive.write["new_hello.txt"]
...

>>> with zipfile.ZipFile["hello.zip"] as archive:
..     archive.printdir[]
...
File Name                                        Modified             Size
hello.txt                                 2021-09-07 19:50:10           83
new_hello.txt                             2021-08-31 17:13:44           13
18 thuận tiện khi bạn cần một cách nhanh chóng và cấp cao để tạo các tệp ZIP của mình bằng Python

Loại bỏ các quảng cáo

Sự kết luận

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 của Python là một công cụ hữu ích khi bạn cần đọc, viết, nén, giải nén và giải nén các tệp từ kho lưu trữ ZIP. Định dạng tệp ZIP đã trở thành một tiêu chuẩn công nghiệp, cho phép bạn đóng gói và tùy ý nén dữ liệu kỹ thuật số của mình

Lợi ích của việc sử dụng tệp ZIP bao gồm lưu trữ các tệp có liên quan với nhau, tiết kiệm dung lượng ổ đĩa, giúp dễ dàng truyền dữ liệu qua mạng máy tính, nhóm mã Python cho mục đích phân phối, v.v.

Trong hướng dẫn này, bạn đã học cách

  • Sử dụng
    >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    3 của Python để đọc, viết và giải nén các tệp ZIP hiện có
  • Đọc siêu dữ liệu về nội dung của các tệp ZIP của bạn với
    >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    3
  • Sử dụng
    >>> import zipfile
    
    >>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
    ..     archive.write["hello.txt"]
    ...
    Traceback [most recent call last]:
        ...
    FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
    
    3 để thao tác các tệp thành viên trong các tệp ZIP hiện có
  • Tạo các tệp ZIP của riêng bạn để lưu trữ và nén dữ liệu kỹ thuật số của bạn

Bạn cũng đã học cách sử dụng

>>> import zipfile

>>> with zipfile.ZipFile["missing/hello.zip", mode="w"] as archive:
..     archive.write["hello.txt"]
...
Traceback [most recent call last]:
    ...
FileNotFoundError: [Errno 2] No such file or directory: 'missing/hello.zip'
3 từ dòng lệnh của mình để liệt kê, tạo và giải nén các tệp ZIP của mình. Với kiến ​​thức này, bạn đã sẵn sàng để lưu trữ, nén và thao tác dữ liệu kỹ thuật số của mình một cách hiệu quả bằng định dạng tệp ZIP

Đánh dấu là đã hoàn thành

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Gửi cho tôi thủ thuật Python »

Giới thiệu về Leodanis Pozo Ramos

Leodanis là một kỹ sư công nghiệp yêu thích Python và phát triển phần mềm. Anh ấy là một nhà phát triển Python tự học với hơn 6 năm kinh nghiệm. Anh ấy là một nhà văn đam mê kỹ thuật với số lượng bài báo được xuất bản ngày càng tăng trên Real Python và các trang web khác

» Tìm hiểu thêm về Leodanis

Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Aldren

Bartosz

Geir Arne

Ian

kate

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi

Chủ Đề