Bài viết này giải thích cách một người có thể thực hiện các thao tác khác nhau trên tệp zip bằng chương trình python đơn giản Tệp zip là gì? ZIP là định dạng tệp lưu trữ hỗ trợ nén dữ liệu không mất dữ liệu. Bằng cách nén không mất dữ liệu, chúng tôi muốn nói rằng thuật toán nén cho phép dữ liệu gốc được tái tạo hoàn hảo từ dữ liệu đã nén. Vì vậy, tệp ZIP là một tệp duy nhất chứa một hoặc nhiều tệp nén, cung cấp một cách lý tưởng để làm cho các tệp lớn nhỏ hơn và giữ các tệp liên quan lại với nhau Tại sao chúng ta cần tệp zip?
Để làm việc trên các tệp zip bằng python, chúng tôi sẽ sử dụng mô-đun python có sẵn có tên zipfile 1. Giải nén một tập tin zip with ZipFile(file_name, 'r') as zip:1 with ZipFile(file_name, 'r') as zip:2 with ZipFile(file_name, 'r') as zip:3 with ZipFile(file_name, 'r') as zip:4 with ZipFile(file_name, 'r') as zip:5 with ZipFile(file_name, 'r') as zip:6 with ZipFile(file_name, 'r') as zip:7 with ZipFile(file_name, 'r') as zip:8 with ZipFile(file_name, 'r') as zip:9 zip.printdir()0 with ZipFile(file_name, 'r') as zip:6 zip.printdir()2 zip.printdir()3 zip.printdir()4____95 zip.printdir()6 zip.printdir()7 zip.printdir()8 zip.printdir()9 zip.printdir()8 zip.printdir()6____212 with ZipFile(file_name, 'r') as zip:6 zip.printdir()8 with ZipFile(file_name, 'r') as zip:15 zip.printdir()8 with ZipFile(file_name, 'r') as zip:17____218 with ZipFile(file_name, 'r') as zip:19 with ZipFile(file_name, 'r') as zip:90 _______98____96____293 zip.printdir()8 with ZipFile(file_name, 'r') as zip:17____218 with ZipFile(file_name, 'r') as zip:97 with ZipFile(file_name, 'r') as zip:90 Chương trình trên trích xuất một tệp zip có tên “my_python_files. zip” trong cùng thư mục với tập lệnh python này. Hãy để chúng tôi cố gắng hiểu đoạn mã trên
2. Ghi vào tệp zip Hãy xem xét một thư mục (thư mục) với định dạng như vậy Ở đây, chúng ta sẽ cần thu thập dữ liệu toàn bộ thư mục và các thư mục con của nó để lấy danh sách tất cả các đường dẫn tệp trước khi ghi chúng vào tệp zip. with ZipFile(file_name, 'r') as zip:1 with ZipFile(file_name, 'r') as zip:2 with ZipFile(file_name, 'r') as zip:3 with ZipFile(file_name, 'r') as zip:4 with ZipFile(file_name, 'r') as zip:5 with ZipFile(file_name, 'r') as zip:4 with ZipFile(file_name, 'r') as zip:35 with ZipFile(file_name, 'r') as zip:6 with ZipFile(file_name, 'r') as zip:37 with ZipFile(file_name, 'r') as zip:38 with ZipFile(file_name, 'r') as zip:6 zip.printdir()8____561 zip.printdir()8 with ZipFile(file_name, 'r') as zip:63____09 with ZipFile(file_name, 'r') as zip:65 with ZipFile(file_name, 'r') as zip:6 zip.printdir()8 with ZipFile(file_name, 'r') as zip:68 zip.printdir()8 with ZipFile(file_name, 'r') as zip:50 with ZipFile(file_name, 'r') as zip:51 with ZipFile(file_name, 'r') as zip:52 with ZipFile(file_name, 'r') as zip:53 with ZipFile(file_name, 'r') as zip:54 with ZipFile(file_name, 'r') as zip:50 with ZipFile(file_name, 'r') as zip:56______652 with ZipFile(file_name, 'r') as zip:58 with ZipFile(file_name, 'r') as zip:59 with ZipFile(file_name, 'r') as zip:50 with ZipFile(file_name, 'r') as zip:59 with ZipFile(file_name, 'r') as zip:52____09 with ZipFile(file_name, 'r') as zip:54 with ZipFile(file_name, 'r') as zip:59 with ZipFile(file_name, 'r') as zip:56 with ZipFile(file_name, 'r') as zip:6 zip.printdir()8 with ZipFile(file_name, 'r') as zip:59 zip.printdir()8 with ZipFile(file_name, 'r') as zip:61 with ZipFile(file_name, 'r') as zip:62 with ZipFile(file_name, 'r') as zip:6 with ZipFile(file_name, 'r') as zip:37 with ZipFile(file_name, 'r') as zip:65 zip.printdir()8 with ZipFile(file_name, 'r') as zip:67 zip.printdir()8 with ZipFile(file_name, 'r') as zip:69 with ZipFile(file_name, 'r') as zip:9 with ZipFile(file_name, 'r') as zip:01 with ZipFile(file_name, 'r') as zip:6 zip.printdir()8 with ZipFile(file_name, 'r') as zip:04 zip.printdir()8 with ZipFile(file_name, 'r') as zip:63____09 with ZipFile(file_name, 'r') as zip:08 with ZipFile(file_name, 'r') as zip:6 zip.printdir()8 with ZipFile(file_name, 'r') as zip:11 zip.printdir()8 with ZipFile(file_name, 'r') as zip:17____218 with ZipFile(file_name, 'r') as zip:15 with ZipFile(file_name, 'r') as zip:90 zip.printdir()8 with ZipFile(file_name, 'r') as zip:50 with ZipFile(file_name, 'r') as zip:8 with ZipFile(file_name, 'r') as zip:52 with ZipFile(file_name, 'r') as zip:21 with ZipFile(file_name, 'r') as zip:54 with ZipFile(file_name, 'r') as zip:17 with ZipFile(file_name, 'r') as zip:24 with ZipFile(file_name, 'r') as zip:6 zip.printdir()8 with ZipFile(file_name, 'r') as zip:27 zip.printdir()8 with ZipFile(file_name, 'r') as zip:29 with ZipFile(file_name, 'r') as zip:30 with ZipFile(file_name, 'r') as zip:31 with ZipFile(file_name, 'r') as zip:32 zip.printdir()5 zip.printdir()6 zip.printdir()7 with ZipFile(file_name, 'r') as zip:54 with ZipFile(file_name, 'r') as zip:37 with ZipFile(file_name, 'r') as zip:54 with ZipFile(file_name, 'r') as zip:50 with ZipFile(file_name, 'r') as zip:40 with ZipFile(file_name, 'r') as zip:52 with ZipFile(file_name, 'r') as zip:21 with ZipFile(file_name, 'r') as zip:59 zip.printdir()6____045 with ZipFile(file_name, 'r') as zip:40 with ZipFile(file_name, 'r') as zip:90 with ZipFile(file_name, 'r') as zip:6 zip.printdir()8 with ZipFile(file_name, 'r') as zip:17____218 with ZipFile(file_name, 'r') as zip:52 with ZipFile(file_name, 'r') as zip:53 with ZipFile(file_name, 'r') as zip:6 with ZipFile(file_name, 'r') as zip:6 with ZipFile(file_name, 'r') as zip:56 with ZipFile(file_name, 'r') as zip:57 with ZipFile(file_name, 'r') as zip:9 with ZipFile(file_name, 'r') as zip:9 with ZipFile(file_name, 'r') as zip:60 zip.printdir()7 zip.printdir()8 with ZipFile(file_name, 'r') as zip:63 Đầu ra của chương trình trên trông như thế này Hãy để chúng tôi cố gắng hiểu đoạn mã trên bằng cách chia thành các đoạn
3. Nhận tất cả thông tin về một tệp zip with ZipFile(file_name, 'r') as zip:1 with ZipFile(file_name, 'r') as zip:2 with ZipFile(file_name, 'r') as zip:3 with ZipFile(file_name, 'r') as zip:4 with ZipFile(file_name, 'r') as zip:5 with ZipFile(file_name, 'r') as zip:4 with ZipFile(file_name, 'r') as zip:70 with ZipFile(file_name, 'r') as zip:6 with ZipFile(file_name, 'r') as zip:7 with ZipFile(file_name, 'r') as zip:8 with ZipFile(file_name, 'r') as zip:9 with ZipFile(file_name, 'r') as zip:75 with ZipFile(file_name, 'r') as zip:6 zip.printdir()2 zip.printdir()3 zip.printdir()4____95 zip.printdir()6 zip.printdir()7 zip.printdir()8 with ZipFile(file_name, 'r') as zip:50 with ZipFile(file_name, 'r') as zip:85 with ZipFile(file_name, 'r') as zip:52 zip.printdir()6 with ZipFile(file_name, 'r') as zip:88 with ZipFile(file_name, 'r') as zip:59 with ZipFile(file_name, 'r') as zip:17 with ZipFile(file_name, 'r') as zip:91 with ZipFile(file_name, 'r') as zip:59 with ZipFile(file_name, 'r') as zip:17 with ZipFile(file_name, 'r') as zip:18 with ZipFile(file_name, 'r') as zip:95 with ZipFile(file_name, 'r') as zip:96 with ZipFile(file_name, 'r') as zip:97 with ZipFile(file_name, 'r') as zip:98 with ZipFile(file_name, 'r') as zip:99 zip.printdir()00 with ZipFile(file_name, 'r') as zip:59 with ZipFile(file_name, 'r') as zip:17 with ZipFile(file_name, 'r') as zip:18 zip.printdir()04 with ZipFile(file_name, 'r') as zip:96 with ZipFile(file_name, 'r') as zip:97 zip.printdir()07 with ZipFile(file_name, 'r') as zip:96 zip.printdir()09 with ZipFile(file_name, 'r') as zip:90 with ZipFile(file_name, 'r') as zip:59 with ZipFile(file_name, 'r') as zip:17 with ZipFile(file_name, 'r') as zip:18 zip.printdir()14 with ZipFile(file_name, 'r') as zip:96 with ZipFile(file_name, 'r') as zip:97 zip.printdir()17 with ZipFile(file_name, 'r') as zip:59 with ZipFile(file_name, 'r') as zip:17 with ZipFile(file_name, 'r') as zip:18 zip.printdir()21 with ZipFile(file_name, 'r') as zip:96 with ZipFile(file_name, 'r') as zip:97 zip.printdir()24 with ZipFile(file_name, 'r') as zip:96 zip.printdir()26 with ZipFile(file_name, 'r') as zip:90 with ZipFile(file_name, 'r') as zip:59 with ZipFile(file_name, 'r') as zip:17 with ZipFile(file_name, 'r') as zip:18 zip.printdir()31 with ZipFile(file_name, 'r') as zip:96 with ZipFile(file_name, 'r') as zip:97 zip.printdir()34 with ZipFile(file_name, 'r') as zip:96 zip.printdir()26 with ZipFile(file_name, 'r') as zip:90 Đầu ra của chương trình trên có thể trông như thế này with ZipFile(file_name, 'r') as zip:0 Ở đây, phương thức infolist() tạo một thể hiện của lớp ZipInfo chứa tất cả thông tin về tệp zip. Bài viết này được đóng góp bởi Nikhil Kumar. Nếu bạn thích GeeksforGeeks và muốn đóng góp, bạn cũng có thể viết một bài báo bằng cách sử dụng write. chuyên viên máy tính. org hoặc gửi bài viết của bạn tới review-team@geeksforgeeks. tổ chức. Xem bài viết của bạn xuất hiện trên trang chính của GeeksforGeeks và trợ giúp các Geeks khác Vui lòng viết bình luận nếu bạn thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề thảo luận ở trên Làm cách nào để giải nén tệp ZIP bằng Python?Python3. # vào một địa điểm cụ thể. Nhập mô-đun zipfile Tạo đối tượng tệp zip bằng lớp ZipFile. Gọi phương thức extract() trên đối tượng tệp zip và chuyển tên của tệp cần giải nén và đường dẫn nơi tệp cần được giải nén và Giải nén tệp cụ thể có trong zip< . .
Làm cách nào để cài đặt gói tệp ZIP trong Python?Để biết thêm thông tin, hãy xem Python Setuptools. . Tải xuống tệp zip pynrfjprog Giải nén tệp zip đã nén và mở cửa sổ Dấu nhắc Lệnh trong thư mục đó Nhập thiết lập python. py cài đặt tại Dấu nhắc Lệnh. Nội dung của gói sẽ được thêm vào thư mục mặc định của Python |