Làm cách nào để chuyển đổi tệp zip thành python?

Các tệp zip rất tốt để nén dữ liệu và được sử dụng trong nhiều trường hợp khác nhau. Nếu bạn đang xây dựng tập lệnh Python và cần tạo hoặc giải nén các tệp ZIP Python, thì bạn thật may mắn

Trong hướng dẫn này, bạn sẽ học cách nén và giải nén (giải nén) các tệp zip, tất cả đều bằng Python

Bắt đầu nào

điều kiện tiên quyết

Nếu bạn muốn làm theo hướng dẫn này, hãy chắc chắn rằng bạn có những thứ sau

  • Trăn 3
  • Một trình soạn thảo văn bản, như Mã VS

Có liên quan. Cách tải tệp xuống bằng Python Wget

Tạo tệp Zip bằng Python

Hãy bắt đầu và trước tiên hãy tập trung vào việc nén tệp bằng Python. Hướng dẫn này sẽ bắt đầu bằng phương pháp đơn giản nhất có thể và sẽ dựa trên các kỹ thuật khác nhau

Để nén một tập tin duy nhất

1. Mở trình soạn thảo văn bản yêu thích của bạn

2. Nếu bạn muốn làm theo chính xác hướng dẫn, hãy tạo một thư mục tại ~/pythonzipdemo và tải các tệp BMP này vào đó. Sau khi thực hiện, bạn sẽ có bốn tệp bitmap; . bmp, all_blue. bmp, all_green. bmp và all_red. bmp. Bạn cũng sẽ có một bản demo. py Tập lệnh Python chứa tất cả mã trong hướng dẫn này

Các tệp hướng dẫn là các tệp bitmap, được hưởng lợi rất nhiều từ việc nén. Không phải tất cả các loại tệp cũng sẽ nén

3. Tạo tập lệnh Python mới có tên demo. py. Hướng dẫn này sẽ lưu tập lệnh demo trong thư mục chính hoặc ~/demo. py

4. Trong tập lệnh Python của bạn, hãy bắt đầu viết mã. Đầu tiên, nhập mô-đun

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0. Mô-đun
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0 là mô-đun Python tích hợp có chứa tất cả các chức năng bạn cần để nén và giải nén tệp bằng Python

import zipfile # imports the zipfile module so you can make use of it

5. Tiếp theo, khởi tạo một đối tượng

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0 bằng cách sử dụng phương thức
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
3, mở tệp zip ở chế độ ghi (
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
4), thêm một tệp vào đó, sau đó đóng đối tượng (
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
5)

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()

Hướng dẫn này dựa trên các đường dẫn tương đối. Đường dẫn tương đối tìm kiếm các tệp trong thư mục hiện tại, nơi Python hiện đang chạy. Thay vào đó, bạn có thể chỉ định đường dẫn tuyệt đối khi chỉ định tên tệp

Khi bạn chạy tập lệnh, bây giờ bạn sẽ thấy một tệp có tên là single_file. nén trong thư mục. chúc mừng. Bạn đã nén tệp đầu tiên của mình. Nhưng đợi đã. Lưu ý rằng nó không nhỏ hơn. Theo mặc định, tệp zip không được nén. Để nén các file bên trong, bạn phải thực hiện thêm một bước nữa

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0 là trình quản lý bối cảnh, bạn cũng có thể sử dụng câu lệnh
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
7 e. g

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
8
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
9

6. Cuối cùng, thêm tham số

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
20 khi khởi tạo đối tượng
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0. Mô-đun
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0 sử dụng các thuộc tính nén khác nhau như ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2 và ZIP_LZMA chỉ ra cách Python nén các tệp bên trong. Theo mặc định, thuộc tính đó được đặt thành
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
23, nghĩa là không nén

Để nén các tệp bên trong kho lưu trữ, bạn phải chỉ định thuộc tính

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
24, như hình bên dưới

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
2

Khi bạn chạy tập lệnh này, bây giờ bạn sẽ thấy rằng single_file. zip nhỏ hơn nhiều so với tệp bitmap thực tế

Áp dụng bộ lọc khi nén tệp

Rất nhiều về việc nén các tệp đã được đề cập, nhưng chỉ bằng cách tham khảo các tệp theo cách thủ công. Khi bạn phải xử lý một số lượng lớn tệp, việc chỉ định từng tệp theo cách thủ công là không lý tưởng

Bạn có thể lọc các tệp bạn muốn hoạt động dựa trên các bộ lọc. Ví dụ: bộ lọc có thể dựa trên kích thước tệp hoặc tên tệp

Không có cách tích hợp nào để lọc tệp được thêm vào tệp zip. Bạn sẽ cần xây dựng giải pháp của riêng mình. Một cách để xây dựng giải pháp của riêng bạn là tạo một hàm Python. Hàm Python là một bộ mã bạn có thể thực thi như một. Xây dựng một chức năng là một cách tuyệt vời để “đóng gói” code thành một đơn vị duy nhất

Có liên quan. Bắt đầu. Hàm Python cho người mới

Chức năng để xây dựng cho nhiệm vụ này sẽ bao gồm khoảng bốn giai đoạn riêng biệt

  1. Mở tệp zip ở chế độ ghi
  2. Đọc tất cả các tệp trong thư mục nguồn mà bạn muốn thêm tệp vào tệp zip từ đó
  3. Lặp lại từng tệp và kiểm tra xem nó có khớp với một chuỗi cụ thể không
  4. Nếu tệp khớp với chuỗi, hãy thêm nó vào tệp zip

Bạn có thể xem chức năng này bên dưới. Chức năng này phức tạp hơn những gì bạn có thể quen dùng nhưng nó cho thấy Python linh hoạt như thế nào, cho phép bạn tạo bất cứ thứ gì bạn cần

Một

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
25 trong Python được sử dụng để tạo các hàm ẩn danh nhỏ. Các chuỗi không thể gọi được và để làm cho
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
26 có thể gọi được;

import zipfile # imports the zipfile module so you can make use of it
2

Liệt kê nội dung của tệp Zip

Khi bạn đã tạo tệp zip, bạn cũng có thể sử dụng Python để đọc các tệp bên trong. Để làm điều đó, hãy sử dụng phương pháp

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
29. Phương thức
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
29 là một cách thuận tiện để truy vấn tất cả các tệp trong tệp zip được trả về dưới dạng một mảng tên tệp

Như được hiển thị bên dưới, xây dựng từ ví dụ trước, gọi phương thức

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
29 trên đối tượng
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0

import zipfile # imports the zipfile module so you can make use of it
7

Bạn sẽ thấy rằng

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
29 trả về một mảng của từng tệp trong tệp zip. Tại thời điểm này, bạn sẽ chỉ thấy một tệp (all_black. bmp) trong tệp nén

Làm cách nào để chuyển đổi tệp zip thành python?
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
29 trả về một mảng của từng tệp trong tệp zip

Nếu bạn muốn đầu ra tên tệp dễ đọc hơn với con người, hãy sử dụng vòng lặp Python để đọc từng tên tệp và đặt một tab ở giữa mỗi tên, như được hiển thị bên dưới

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0

Thêm tệp vào tệp Zip hiện có

Vì vậy, bạn đã có một tệp zip và muốn thêm một số tệp vào đó. Không vấn đề gì. Bạn chỉ cần thay đổi chế độ mà bạn mở tệp zip trong

Để thêm tệp vào tệp zip hiện có, hãy mở tệp zip ở chế độ chắp thêm (

import zipfile # imports the zipfile module so you can make use of it
25), sau đó gọi phương thức
import zipfile # imports the zipfile module so you can make use of it
26 chuyển tệp để thêm vào tệp zip, như minh họa bên dưới

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
3

Trích xuất nội dung của tệp Zip bằng Mô-đun ## Instantiate a new zipfile object creating the single_zip.zip archive. zf = zipfile.ZipFile('single_file.zip', mode='w') ## Add a file to the archive zf.write('all_black.bmp') ## Close the archive releasing it from memory zf.close()0

Hãy chuyển sang giải nén tệp từ các tệp zip hiện có. Sử dụng phương pháp

import zipfile # imports the zipfile module so you can make use of it
28 và
import zipfile # imports the zipfile module so you can make use of it
29, bạn có thể biến điều đó thành hiện thực

Ví dụ dưới đây đang mở zip để đọc (

import zipfile # imports the zipfile module so you can make use of it
70) sau đó giải nén một tệp (
import zipfile # imports the zipfile module so you can make use of it
71) từ zip vào thư mục nhà của bạn. Nó cũng minh họa cách sử dụng phương pháp
import zipfile # imports the zipfile module so you can make use of it
29 để giải nén tất cả các tệp trong zip vào thư mục chính của bạn

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0

Trích xuất nội dung của tệp Zip Python bằng mô-đun import zipfile # imports the zipfile module so you can make use of it73

Để thay thế cho việc sử dụng mô-đun

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0, bạn cũng có thể sử dụng một mô-đun Python tích hợp khác có tên là
import zipfile # imports the zipfile module so you can make use of it
75.
import zipfile # imports the zipfile module so you can make use of it
73 là một mô-đun không tập trung cụ thể vào các tệp zip mà quản lý tệp chung hơn. Tình cờ là có một số phương pháp tệp zip tiện dụng để sử dụng

Để trích xuất nội dung của các tệp zip bằng mô-đun

import zipfile # imports the zipfile module so you can make use of it
73, hãy cung cấp tệp zip và đường dẫn để giải nén tất cả các tệp vào. Không giống như phương pháp
import zipfile # imports the zipfile module so you can make use of it
28 trên mô-đun
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0, phương pháp
## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
00 của
import zipfile # imports the zipfile module so you can make use of it
73 không cho phép bạn chọn tệp từ kho lưu trữ để giải nén

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0

Áp dụng bộ lọc khi giải nén tệp

Như đã trình bày trong phần lọc zip, giờ hãy áp dụng phương pháp tương tự (tạo hàm) để giải nén các tệp khớp với một bộ lọc cụ thể. Bạn có thể xem ví dụ chức năng bên dưới. Hàm này đọc một tệp zip, đọc tất cả các tệp bên trong tệp zip, lặp lại từng tệp và nếu tệp đó khớp với bộ lọc đã xác định, nó sẽ chuyển tệp đó sang phương thức

import zipfile # imports the zipfile module so you can make use of it
28

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
2

Làm cách nào để chuyển đổi tệp zip thành python?
Kết quả chạy chức năng cuối cùng của bài viết

Phần kết luận

Bây giờ bạn đã hiểu rõ về cách làm việc với các tệp zip trong Python. Sử dụng cả mô-đun

## Instantiate a new zipfile object creating the single_zip.zip archive.
zf = zipfile.ZipFile('single_file.zip', mode='w')

## Add a file to the archive
zf.write('all_black.bmp')

## Close the archive releasing it from memory
zf.close()
0 và
import zipfile # imports the zipfile module so you can make use of it
73, cùng với việc xây dựng một số hàm Python, giờ đây bạn sẽ biết cách quản lý bất kỳ tác vụ tệp zip nào được ném vào bạn

Bạn sẽ tích hợp kiến ​​thức mới này vào dự án Python tiếp theo của mình như thế nào?

Ghét quảng cáo?

Khám phá sách hướng dẫn ATA

Thông tin khác từ ATA Learning & Partners

  • Làm cách nào để chuyển đổi tệp zip thành python?

    Tài nguyên được đề xuất

    Tài nguyên được đề xuất cho đào tạo, bảo mật thông tin, tự động hóa, v.v.

  • Làm cách nào để chuyển đổi tệp zip thành python?

    Được trả tiền để viết

    ATA Learning luôn tìm kiếm những người hướng dẫn ở mọi cấp độ kinh nghiệm. Bất kể bạn là quản trị viên cấp dưới hay kiến ​​trúc sư hệ thống, bạn đều có điều gì đó để chia sẻ. Tại sao không viết trên một nền tảng có khán giả hiện có và chia sẻ kiến ​​thức của bạn với mọi người?

  • Làm cách nào để chuyển đổi tệp zip thành python?

    Sách hướng dẫn học tập ATA

    ATA Learning được biết đến với các hướng dẫn bằng văn bản chất lượng cao dưới dạng các bài đăng trên blog. Hỗ trợ Học tập ATA với sách điện tử PDF Sách hướng dẫn ATA khả dụng ngoại tuyến và không có quảng cáo

    Làm cách nào để cài đặt 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

    Làm cách nào để đọc tệp zip trong gấu trúc Python?

    Phương pháp #1. Sử dụng nén=zip trong gấu trúc. phương thức read_csv() . Bằng cách gán đối số nén trong phương thức read_csv() dưới dạng zip, trước tiên, gấu trúc sẽ giải nén zip và sau đó sẽ tạo khung dữ liệu từ tệp CSV có trong tệp nén.