Dưới đây là một số mã để zipping toàn bộ thư mục thành một zipfile.
Điều này dường như hoạt động OK khi tạo các tệp zip trên cả Windows và Linux. Các tệp đầu ra dường như trích xuất đúng cách trên Windows [tính năng thư mục nén tích hợp, Winzip và 7-Zip] và Linux. Tuy nhiên, các thư mục trống trong một tệp zip dường như là một vấn đề gai góc. Giải pháp dưới đây dường như hoạt động nhưng đầu ra của "zipinfo" trên Linux có liên quan. Ngoài ra, các quyền thư mục không được đặt chính xác cho các thư mục trống trong kho lưu trữ zip. Điều này dường như đòi hỏi một số nghiên cứu sâu hơn.
Tôi đã nhận được một số thông tin từ chủ đề đánh giá vận tốc này và chủ đề danh sách gửi thư Python này.
Lưu ý rằng chức năng này được thiết kế để đưa các tệp vào kho lưu trữ zip không có thư mục cha hoặc chỉ một thư mục cha, do đó, nó sẽ cắt bất kỳ thư mục hàng đầu nào trong các đường dẫn hệ thống tệp và không bao gồm chúng bên trong các đường dẫn lưu trữ zip. Đây thường là trường hợp khi bạn muốn chỉ lấy một thư mục và biến nó thành một tệp zip có thể được trích xuất ở các vị trí khác nhau.
Từ khóa đối số:
Dirpath - Đường dẫn chuỗi đến thư mục để lưu trữ. Đây là đối số duy nhất cần thiết. Nó có thể là tuyệt đối hoặc tương đối, nhưng chỉ có một hoặc không có thư mục hàng đầu sẽ được đưa vào kho lưu trữ zip.
ZipFilePath - Đường dẫn chuỗi đến tệp ZIP đầu ra. Đây có thể là một con đường tuyệt đối hoặc tương đối. Nếu tệp zip đã tồn tại, nó sẽ được cập nhật. Nếu không, nó sẽ được tạo ra. Nếu bạn muốn thay thế nó từ đầu, hãy xóa nó trước khi gọi chức năng này. [Mặc định được tính toán dưới dạng dirpath + ".zip"]
Bao gồmIrinzip - Boolean cho biết liệu thư mục cấp cao nhất có nên được đưa vào kho lưu trữ hay bỏ qua hay không. [mặc định đúng]
.
#!/usr/bin/python
import os
import zipfile
def zipdir[dirPath=None, zipFilePath=None, includeDirInZip=True]:
if not zipFilePath:
zipFilePath = dirPath + ".zip"
if not os.path.isdir[dirPath]:
raise OSError["dirPath argument must point to a directory. "
"'%s' does not." % dirPath]
parentDir, dirToZip = os.path.split[dirPath]
#Little nested function to prepare the proper archive path
def trimPath[path]:
archivePath = path.replace[parentDir, "", 1]
if parentDir:
archivePath = archivePath.replace[os.path.sep, "", 1]
if not includeDirInZip:
archivePath = archivePath.replace[dirToZip + os.path.sep, "", 1]
return os.path.normcase[archivePath]
outFile = zipfile.ZipFile[zipFilePath, "w",
compression=zipfile.ZIP_DEFLATED]
for [archiveDirPath, dirNames, fileNames] in os.walk[dirPath]:
for fileName in fileNames:
filePath = os.path.join[archiveDirPath, fileName]
outFile.write[filePath, trimPath[filePath]]
#Make sure we get empty directories as well
if not fileNames and not dirNames:
zipInfo = zipfile.ZipInfo[trimPath[archiveDirPath] + "/"]
#some web sites suggest doing
#zipInfo.external_attr = 16
#or
#zipInfo.external_attr = 48
#Here to allow for inserting an empty directory. Still TBD/TODO.
outFile.writestr[zipInfo, ""]
outFile.close[]
Đây là một số cách sử dụng mẫu. Lưu ý rằng nếu đối số Dirpath của bạn có một số thư mục hàng đầu, chỉ có tên cuối cùng sẽ được đưa vào theo mặc định. Vượt qua bao gồmIrinzip = false để bỏ qua tất cả các thư mục hàng đầu.
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
Mã nguồn: lib/zipfile.py Lib/zipfile.py
Định dạng tệp ZIP là một tiêu chuẩn lưu trữ và nén phổ biến. Mô -đun này cung cấp các công cụ để tạo, đọc, ghi, nối và liệt kê một tệp zip. Bất kỳ việc sử dụng nâng cao của mô -đun này sẽ yêu cầu sự hiểu biết về định dạng, như được định nghĩa trong ứng dụng PKZIP Lưu ý.
Mã nguồn: lib/zipfile.py
Mô-đun này hiện không xử lý các tệp zip nhiều đĩa. Nó có thể xử lý các tệp zip sử dụng các tiện ích mở rộng zip64 [đó là các tệp zip có kích thước cao hơn 4 GIB]. Nó hỗ trợ giải mã các tệp được mã hóa trong kho lưu trữ zip, nhưng hiện tại nó không thể tạo một tệp được mã hóa. Giải mã cực kỳ chậm vì nó được thực hiện trong Python bản địa chứ không phải C.
Mô -đun xác định các mục sau:Ngoại lệ ________ 11 ________ 12¶
Lỗi được nêu ra cho các tệp zip xấu.
Mới trong phiên bản 3.2.Ngoại lệ ________ 11 ________ 14¶
Bí danh của
5, để tương thích với các phiên bản Python cũ hơn.zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
Ngoại lệ ________ 11 ________ 17¶
Lỗi được nêu ra khi tệp zip yêu cầu chức năng zip64 nhưng điều đó chưa được bật.Lớp
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
1zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
9ZipFile Objects for constructor details.Lớp để đọc và viết các tập tin zip. Xem phần đối tượng Zipfile để biết chi tiết về hàm tạo.Lớp
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
1with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']1Path Objects for details.
Một trình bao bọc tương thích pathlib cho các tệp zip. Xem phần đối tượng đường dẫn để biết chi tiết.
Mới trong phiên bản 3.8.Lớp
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
1with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']3Lớp để tạo tài liệu lưu trữ zip có chứa các thư viện Python.[filename='NoName', date_time=[1980, 1, 1, 0, 0, 0]]¶
Lớp ________ 11 ________ 25 [fileName = 'noname', date_time = [1980, 1, 1, 0, 0, 0]] ¶ZipInfo Objects.
Lớp được sử dụng để đại diện cho thông tin về một thành viên của một kho lưu trữ. Các trường hợp của lớp này được trả về bởi các phương thứcwith ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']6 và
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']7 của các đối tượng
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']8. Hầu hết người dùng của mô -đun
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']9 sẽ không cần phải tạo các mô -đun này, mà chỉ sử dụng những người được tạo bởi mô -đun này. Tên tệp phải là tên đầy đủ của thành viên lưu trữ và Date_Time phải là một bộ chứa sáu trường mô tả thời gian sửa đổi cuối cùng của tệp; Các trường được mô tả trong phần đối tượng Zipinfo.[filename]¶
________ 11 ________ 31 [Tên tệp] ¶
Trả về with ZipFile['spam.zip'] as myzip:
with myzip.open['eggs.txt'] as myfile:
print[myfile.read[]]
2 Nếu tên tệp là tệp zip hợp lệ dựa trên số phép thuật của nó, nếu không, sẽ trả về with ZipFile['spam.zip'] as myzip:
with myzip.open['eggs.txt'] as myfile:
print[myfile.read[]]
3. Tên tệp có thể là một đối tượng giống như tệp hoặc tệp.Support for file and file-like objects.
________ 11 ________ 35¶
Hằng số số cho một thành viên lưu trữ không nén.________ 11 ________ 37¶
Hằng số số cho phương pháp nén zip thông thường. Điều này đòi hỏi mô -đunwith ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]8.
________ 11 ________ 40¶
Hằng số số cho phương pháp nén BZIP2. Điều này yêu cầu mô -đun >>> Path[...].joinpath['child'].joinpath['grandchild']
>>> Path[...].joinpath['child', 'grandchild']
>>> Path[...] / 'child' / 'grandchild'
1.
________ 11 ________ 43¶
Hằng số số cho phương pháp nén BZIP2. Điều này yêu cầu mô -đun >>> Path[...].joinpath['child'].joinpath['grandchild']
>>> Path[...].joinpath['child', 'grandchild']
>>> Path[...] / 'child' / 'grandchild'
1.
Mới trong phiên bản 3.3.
________ 11 ________ 43¶
Hằng số số cho phương pháp nén LZMA. Điều này đòi hỏi mô -đun
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'4.Ghi chú
Thông số kỹ thuật định dạng tệp ZIP đã bao gồm hỗ trợ nén BZIP2 kể từ năm 2001 và để nén LZMA từ năm 2006. Tuy nhiên, một số công cụ [bao gồm các bản phát hành Python cũ hơn] không hỗ trợ các phương thức nén này và có thể từ chối xử lý tệp ZIP hoàn toàn hoặc hoặc Không trích xuất các tệp riêng lẻ.
Xem thêmGhi chú ứng dụng PKZIP
Tài liệu về định dạng tệp zip của Phil Katz, người tạo định dạng và thuật toán được sử dụng.
Trang chủ thông tin-Zip[file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True]¶Thông tin về các chương trình lưu trữ và thư viện phát triển dự án thông tin-ZIP.path-like object.
Đối tượng Zipfile
Nén là phương pháp nén zip để sử dụng khi viết kho lưu trữ và phải là
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]8,
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]9,
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile0 hoặc
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile1; Các giá trị không được công nhận sẽ khiến
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile2 được nâng lên. Nếu
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]9,
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile0 hoặc
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile1 được chỉ định nhưng mô -đun tương ứng [
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]8,
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'1 hoặc
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'4] không có sẵn,
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile9 được nâng lên. Mặc định là
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]8.
Nếu ZipFile cho phép là
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]2 [mặc định] sẽ tạo các tệp zip sử dụng các tiện ích mở rộng zip64 khi zipfile lớn hơn 4 GIB. Nếu đó là
$ python -m zipfile -c monty.zip spam.txt eggs.txt
2 with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']9 sẽ tăng một ngoại lệ khi tệp zip sẽ yêu cầu tiện ích mở rộng zip64.
Tham số nén kiểm soát mức nén để sử dụng khi ghi tệp vào kho lưu trữ. Khi sử dụng
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]8 hoặc
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile1, nó không có tác dụng. Khi sử dụng số nguyên
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]9
$ python -m zipfile -c monty.zip spam.txt eggs.txt
7 đến $ python -m zipfile -c monty.zip spam.txt eggs.txt
8 được chấp nhận [xem with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]8 để biết thêm thông tin]. Khi sử dụng số nguyên
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile0
$ python -m zipfile -c monty.zip life-of-brian_1979/
1 đến $ python -m zipfile -c monty.zip spam.txt eggs.txt
8 được chấp nhận [xem >>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'1 để biết thêm thông tin].
Đối số StricT_Timestamp, khi được đặt thành
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]3, cho phép zip các tệp cũ hơn 1980-01-01 với chi phí đặt dấu thời gian thành 1980-01-01. Hành vi tương tự xảy ra với các tệp mới hơn 2107-12-31, dấu thời gian cũng được đặt ở giới hạn.
Nếu tệp được tạo bằng chế độ
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'8,
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]0 hoặc
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'9 và sau đó
$ python -m zipfile -c monty.zip life-of-brian_1979/
8 mà không cần thêm bất kỳ tệp nào vào kho lưu trữ, các cấu trúc zip thích hợp cho một kho lưu trữ trống sẽ được ghi vào tệp.ZipFile cũng là một người quản lý bối cảnh và do đó hỗ trợ tuyên bố
$ python -m zipfile -c monty.zip life-of-brian_1979/
9. Trong ví dụ, Myzip bị đóng sau khi bộ câu lệnh $ python -m zipfile -c monty.zip life-of-brian_1979/
9 đã hoàn thành, ngay cả khi một ngoại lệ xảy ra:with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']
Mới trong phiên bản 3.2: Đã thêm khả năng sử dụng with ZipFile['spam.zip', 'w'] as myzip:
myzip.write['eggs.txt']
8 làm trình quản lý ngữ cảnh.Added the ability to use
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']8 as a context manager.
Đã thay đổi trong phiên bản 3.3: Đã thêm hỗ trợ cho nén $ python -m zipfile -e monty.zip target-dir/
2 và >>> Path[...].joinpath['child'].joinpath['grandchild']
>>> Path[...].joinpath['child', 'grandchild']
>>> Path[...] / 'child' / 'grandchild'
4.Added support for
$ python -m zipfile -e monty.zip target-dir/
2 and >>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'4 compression.
Đã thay đổi trong phiên bản 3.4: Tiện ích mở rộng ZIP64 được bật theo mặc định.ZIP64 extensions are enabled by default.
Đã thay đổi trong phiên bản 3.5: Đã thêm hỗ trợ để viết vào các luồng không thể nhìn thấy. Đã thêm hỗ trợ cho chế độ >>> zf = PyZipFile['myprog.zip']
>>> def notests[s]:
... fn = os.path.basename[s]
... return [not [fn == 'test' or fn.startswith['test_']]]
>>> zf.writepy['myprog', filterfunc=notests]
0.Added support for writing to
unseekable streams. Added support for the
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]0 mode.
Đã thay đổi trong phiên bản 3.6: Trước đây, một string.pyc # Top level name
test/__init__.pyc # Package directory
test/testall.pyc # Module test.testall
test/bogus/__init__.pyc # Subpackage directory
test/bogus/myfile.pyc # Submodule test.bogus.myfile
9 đơn giản đã được nâng lên cho các giá trị nén không được công nhận.Previously, a plain
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile9 was raised for unrecognized compression values.
Đã thay đổi trong phiên bản 3.6.2: Tham số tệp chấp nhận một đối tượng giống như đường dẫn.The file parameter accepts a path-like object.
Đã thay đổi trong phiên bản 3.7: Thêm tham số nén.Add the compresslevel parameter.
Mới trong Phiên bản 3.8: Đối số chỉ từ khóa của Strict_TimestampsThe strict_timestamps keyword-only argument
________ 96 ________ 97 [][]¶Đóng tệp lưu trữ. Bạn phải gọi
$ python -m zipfile -e monty.zip target-dir/
8 trước khi thoát khỏi chương trình hoặc hồ sơ cần thiết của bạn sẽ không được viết. ________ 96 ________ 100 [tên][name]¶Trả về một đối tượng
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01 với thông tin về tên thành viên lưu trữ. Gọi with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']6 cho một tên hiện không có trong kho lưu trữ sẽ tăng
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
03. ________ 96 ________ 105 [][]¶Trả về một danh sách chứa một đối tượng
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01 cho mỗi thành viên của kho lưu trữ. Các đối tượng theo thứ tự giống như các mục của chúng trong tệp zip thực tế trên đĩa nếu một kho lưu trữ hiện có được mở. ________ 96 ________ 108 [][]¶Trả lại một danh sách các thành viên lưu trữ theo tên.
________ 96 ________ 110 [Tên, MODE = 'R', PWD = Không, *, Force_Zip64 = Sai[name, mode='r', pwd=None, *, force_zip64=False]¶Truy cập một thành viên của kho lưu trữ dưới dạng đối tượng giống như tệp nhị phân. Tên có thể là tên của một tệp trong kho lưu trữ hoặc đối tượng
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01. Tham số chế độ, nếu được bao gồm, phải là >>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'7 [mặc định] hoặc
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'8. NKT là mật khẩu được sử dụng để giải mã các tệp zip được mã hóa.
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
14 cũng là người quản lý bối cảnh và do đó hỗ trợ tuyên bố $ python -m zipfile -c monty.zip life-of-brian_1979/
9:with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]
Với chế độ
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'7 đối tượng giống như tệp [
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
17] chỉ đọc và cung cấp các phương pháp sau: zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
18, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
19, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
20, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
21, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
22, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
23, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
24. Các đối tượng này có thể hoạt động độc lập với zipfile.Với
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
25, một tay cầm tệp có thể ghi được trả về, hỗ trợ phương thức zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
26. Mặc dù xử lý tệp có thể ghi được mở, việc cố gắng đọc hoặc ghi các tệp khác trong tệp zip sẽ tăng zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
27.Khi viết một tệp, nếu kích thước tệp không được biết trước nhưng có thể vượt quá 2 Gib, hãy vượt qua
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
28 để đảm bảo định dạng tiêu đề có khả năng hỗ trợ các tệp lớn. Nếu kích thước tệp được biết trước, hãy xây dựng một đối tượng zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01 với bộ zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
30 và sử dụng nó làm tham số tên.Ghi chú
Các phương thức
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
14, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
18 và zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
33 có thể lấy tên tệp hoặc đối tượng zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01. Bạn sẽ đánh giá cao điều này khi cố gắng đọc một tệp zip có chứa các thành viên có tên trùng lặp.Đã thay đổi trong phiên bản 3.6:
35 hiện có thể được sử dụng để ghi các tệp vào kho lưu trữ với tùy chọn zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
25.zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
35 can now be used to write files into the archive with the zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
25 option.Đã thay đổi trong phiên bản 3.6: Gọi
14 trên Zipfile đóng sẽ tăng zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
27. Trước đây, một zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
string.pyc # Top level name
test/__init__.pyc # Package directory
test/testall.pyc # Module test.testall
test/bogus/__init__.pyc # Subpackage directory
test/bogus/myfile.pyc # Submodule test.bogus.myfile
9 đã được nâng lên.Calling
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
14 on a
closed ZipFile will raise a zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
27. Previously, a string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile9 was raised.
Trích xuất một thành viên từ kho lưu trữ đến thư mục làm việc hiện tại; Thành viên phải là tên đầy đủ của nó hoặc đối tượng
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01. Thông tin tệp của nó được trích xuất chính xác nhất có thể. Đường dẫn chỉ định một thư mục khác nhau để trích xuất. Thành viên có thể là một tên tệp hoặc đối tượng zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01. NKT là mật khẩu được sử dụng cho các tệp được mã hóa.Trả về đường dẫn được chuẩn hóa được tạo [một thư mục hoặc tệp mới].
Ghi chú
Nếu một tên tệp thành viên là một đường dẫn tuyệt đối, thì ổ đĩa Drive/UNC SharePoint và Dẫn [trở lại] sẽ bị tước, ví dụ:
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
42 trở thành zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
43 trên UNIX và zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
44 trở thành zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
45 trên Windows. Và tất cả các thành phần zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
46 trong tên tệp thành viên sẽ bị xóa, ví dụ: zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
47 trở thành zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
48. Trên các ký tự bất hợp pháp của Windows [zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
49, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
50, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
51, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
52, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
53, zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
54 và zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
55] được thay thế bằng dấu gạch dưới [zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
56].Đã thay đổi trong phiên bản 3.6: Gọi
33 trên Zipfile đóng sẽ tăng zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
27. Trước đây, một zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
string.pyc # Top level name
test/__init__.pyc # Package directory
test/testall.pyc # Module test.testall
test/bogus/__init__.pyc # Subpackage directory
test/bogus/myfile.pyc # Submodule test.bogus.myfile
9 đã được nâng lên.Calling
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
33 on a closed ZipFile will raise a zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
27. Previously, a
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile9 was raised.
Thay đổi trong phiên bản 3.6.2: Tham số đường dẫn chấp nhận một đối tượng giống như đường dẫn.The path parameter accepts a path-like object.
Trích xuất tất cả các thành viên từ kho lưu trữ đến thư mục làm việc hiện tại. Đường dẫn chỉ định một thư mục khác nhau để trích xuất. Các thành viên là tùy chọn và phải là một tập hợp con của danh sách được trả về bởi
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
60. NKT là mật khẩu được sử dụng cho các tệp được mã hóa.Cảnh báo
Không bao giờ trích xuất tài liệu lưu trữ từ các nguồn không tin cậy mà không kiểm tra trước. Có thể các tệp được tạo bên ngoài đường dẫn, ví dụ: Các thành viên có tên tệp tuyệt đối bắt đầu bằng
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
61 hoặc tên tệp với hai dấu chấm zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
46. Mô -đun này cố gắng ngăn chặn điều đó. Xem zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
33 Lưu ý.Thay đổi trong phiên bản 3.6.2: Tham số đường dẫn chấp nhận một đối tượng giống như đường dẫn.The path parameter accepts a path-like object.
Trích xuất tất cả các thành viên từ kho lưu trữ đến thư mục làm việc hiện tại. Đường dẫn chỉ định một thư mục khác nhau để trích xuất. Các thành viên là tùy chọn và phải là một tập hợp con của danh sách được trả về bởizipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
60. NKT là mật khẩu được sử dụng cho các tệp được mã hóa.[]¶Cảnh báo
Không bao giờ trích xuất tài liệu lưu trữ từ các nguồn không tin cậy mà không kiểm tra trước. Có thể các tệp được tạo bên ngoài đường dẫn, ví dụ: Các thành viên có tên tệp tuyệt đối bắt đầu bằngzipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
61 hoặc tên tệp với hai dấu chấm zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
46. Mô -đun này cố gắng ngăn chặn điều đó. Xem zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
33 Lưu ý.[pwd]¶________ 96 ________ 165 []
In một bảng nội dung cho kho lưu trữ đếnzipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
66.[name,
pwd=None]¶________ 96 ________ 168 [NKT]
Đặt PWD làm mật khẩu mặc định để trích xuất các tệp được mã hóa.Calling
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
18 on a closed ZipFile will raise a zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
27. Previously, a string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile9 was raised. ________ 96 ________ 170 [Tên, PWD = Không] ¶[]¶
Trả về các byte của tên tệp trong kho lưu trữ. Tên là tên của tệp trong kho lưu trữ hoặc đối tượng
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01. Lưu trữ phải được mở để đọc hoặc nối. PWD là mật khẩu được sử dụng cho các tệp được mã hóa và, nếu được chỉ định, nó sẽ ghi đè mật khẩu mặc định được đặt với zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
72. Gọi zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
18 trên ZipFile sử dụng phương thức nén khác với >>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]8,
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]9,
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile0 hoặc
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile1 sẽ tăng
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile2. Một lỗi cũng sẽ được nêu ra nếu mô -đun nén tương ứng không có sẵn.
Đã thay đổi trong phiên bản 3.6: Gọi
18 trên Zipfile đóng sẽ tăng zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
27. Trước đây, một zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
string.pyc # Top level name
test/__init__.pyc # Package directory
test/testall.pyc # Module test.testall
test/bogus/__init__.pyc # Subpackage directory
test/bogus/myfile.pyc # Submodule test.bogus.myfile
9 đã được nâng lên.Calling
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
85 on a closed ZipFile will raise a zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
27. Previously, a string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile9 was raised. ________ 96 ________ 183 [][filename, arcname=None, compress_type=None, compresslevel=None]¶
Đọc tất cả các tệp trong kho lưu trữ và kiểm tra các tiêu đề CRC và tệp của họ. Trả về tên của tệp xấu đầu tiên, hoặc nếu không trả về
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
84.Ghi chú
Đã thay đổi trong phiên bản 3.6: Gọi
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
85 trên Zipfile đóng sẽ tăng zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
27. Trước đây, một string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile9 đã được nâng lên.
Ghi chú
________ 96 ________ 189 [tên tệp, arcname = none, compress_type = none
Ghi chú
Viết tệp có tên FileName vào kho lưu trữ, đặt cho nó tên lưu trữ arcName [theo mặc định, đây sẽ giống như tên tệp, nhưng không có ký tự ổ đĩa và với các bộ phân cách đường dẫn hàng đầu bị xóa]. Nếu được đưa ra, nén_type ghi đè giá trị được đưa ra cho tham số nén cho hàm tạo cho mục nhập mới. Tương tự, nén sẽ ghi đè hàm tạo nếu được đưa ra. Lưu trữ phải được mở với chế độ
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'8,
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]0 hoặc
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'9.
Tên lưu trữ phải liên quan đến gốc lưu trữ, nghĩa là chúng không nên bắt đầu với một dấu phân cách đường dẫn.Calling
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
26 on a ZipFile created with mode >>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'7 or a closed ZipFile will raise a
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
27. Previously, a string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile9 was raised.Nếu
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
93 [hoặc zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
94, nếu zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
93 không được đưa ra] chứa một byte null, tên của tệp trong kho lưu trữ sẽ bị cắt ở byte null.[zinfo_or_arcname, data, compress_type=None, compresslevel=None]¶Một dấu gạch chéo hàng đầu trong tên tệp có thể dẫn đến việc lưu trữ không thể mở trong một số chương trình zip trên các hệ thống Windows.
Nếu được đưa ra, nén_type ghi đè giá trị được đưa ra cho tham số nén cho hàm tạo cho mục nhập mới hoặc trong zinfo_or_arcname [nếu đó là một ví dụ
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01]. Tương tự, nén sẽ ghi đè hàm tạo nếu được đưa ra.Ghi chú
Khi chuyển một thể hiện
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01 dưới dạng tham số zinfo_or_arcname, phương thức nén được sử dụng sẽ được chỉ định trong thành viên nén_type của thể hiện ____101 đã cho. Theo mặc định, hàm tạo zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01 đặt thành viên này thành >>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]8.
Đã thay đổi trong phiên bản 3.2: Đối số nén_type.The compress_type argument.
Đã thay đổi trong phiên bản 3.6: Gọi with ZipFile['spam.zip', 'w'] as myzip:
myzip.write['eggs.txt']
14 trên zipfile được tạo với chế độ >>> Path[...].joinpath['child'].joinpath['grandchild']
>>> Path[...].joinpath['child', 'grandchild']
>>> Path[...] / 'child' / 'grandchild'
7 hoặc zipfile đóng sẽ tăng
27. Trước đây, một zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
string.pyc # Top level name
test/__init__.pyc # Package directory
test/testall.pyc # Module test.testall
test/bogus/__init__.pyc # Subpackage directory
test/bogus/myfile.pyc # Submodule test.bogus.myfile
9 đã được nâng lên.Calling
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']14 on a ZipFile created with mode
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'7 or a closed ZipFile will raise a
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
27. Previously, a string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile9 was raised.
Các thuộc tính dữ liệu sau đây cũng có sẵn:
________ 96 ________ 219¶Tên của tệp zip.
________ 96 ________ 221¶Mức độ đầu ra gỡ lỗi để sử dụng. Điều này có thể được đặt từ
$ python -m zipfile -c monty.zip spam.txt eggs.txt
7 [mặc định, không có đầu ra] thành with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']23 [đầu ra nhiều nhất]. Thông tin gỡ lỗi được ghi vào
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
66.Nhận xét liên quan đến tệp zip dưới dạng đối tượng
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']03. Nếu gán một nhận xét cho một ví dụ
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']8 được tạo với chế độ
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'8,
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]0 hoặc
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'9, nó sẽ không dài hơn 65535 byte. Nhận xét lâu hơn điều này sẽ bị cắt ngắn.
Đối tượng đường dẫn Sở đối tượng
Lớp ________ 11 ________ 21 [root, at = ''] ¶[root, at='']¶Xây dựng một đối tượng đường dẫn từ
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']32 zipfile [có thể là một ví dụ
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']8 hoặc
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']34 phù hợp để chuyển đến hàm tạo
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']8].
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']36 Chỉ định vị trí của đường dẫn này trong zipfile, ví dụ: ‘DIR/FILE.TXT,‘ Dir/xông, hoặc ‘. Mặc định cho chuỗi trống, chỉ ra gốc.
Các đối tượng đường dẫn hiển thị các tính năng sau của các đối tượng
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']37:
Các đối tượng đường dẫn có thể đi qua bằng toán tử
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']38 hoặc
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']39. ________ 240 ________ 241¶
Thành phần đường dẫn cuối cùng.
________ 240 ________ 110 [mode = 'r', *, pwd, **] ¶[mode='r', *, pwd, **]¶Gọi
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
35 trên đường dẫn hiện tại. Cho phép mở để đọc hoặc viết, văn bản hoặc nhị phân thông qua các chế độ được hỗ trợ: ‘R,‘ W, ‘RB,‘ WB. Đối số vị trí và từ khóa được truyền qua with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']45 khi được mở dưới dạng văn bản và bị bỏ qua.
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']46 là tham số
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']46 thành
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
35.Đã thay đổi trong phiên bản 3.9: Đã thêm hỗ trợ cho các chế độ văn bản và nhị phân để mở. Chế độ mặc định bây giờ là văn bản.Added support for text and binary modes for open. Default mode is now text.
________ 240 ________ 250 [][]¶Liệt kê những đứa trẻ của thư mục hiện tại.
________ 240 ________ 252 [][]¶Trả về
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]2 Nếu bối cảnh hiện tại tham khảo một thư mục. ________ 240 ________ 255 [][]¶
Trả về
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]2 Nếu bối cảnh hiện tại tham khảo một tệp. ________ 240 ________ 258 [][]¶
Trả về
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]2 Nếu bối cảnh hiện tại tham chiếu một tệp hoặc thư mục trong tệp zip. ________ 240 ________ 261 [*, **][*, **]¶
Đọc tệp hiện tại dưới dạng văn bản Unicode. Các đối số vị trí và từ khóa được truyền qua
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']45 [ngoại trừ
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']63, được ngụ ý bởi bối cảnh]. ________ 240 ________ 265 [][]¶
Đọc tệp hiện tại dưới dạng byte.
________ 240 ________ 267 [*Khác] ¶[*other]¶Trả về một đối tượng đường dẫn mới với mỗi đối số khác đã tham gia. Sau đây là tương đương:
>>> Path[...].joinpath['child'].joinpath['grandchild'] >>> Path[...].joinpath['child', 'grandchild'] >>> Path[...] / 'child' / 'grandchild'
Đã thay đổi trong phiên bản 3.10: Trước 3.10, with ZipFile['spam.zip', 'w'] as myzip:
myzip.write['eggs.txt']
39 không được giấy tờ và được chấp nhận chính xác một tham số.Prior to 3.10,
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']39 was undocumented and accepted exactly one parameter.
Đối tượng pyzipfile
Chất xây dựng
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']69 có cùng tham số với hàm tạo
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']8 và một tham số bổ sung, tối ưu hóa. Lớp ________ 11 ________ 23 [Tệp, mode = 'r', nén = zip_stored, allowzip64 = true, tối ưu hóa =- 1] ¶[file, mode='r', compression=ZIP_STORED, allowZip64=True, optimize=- 1]¶
Mới trong phiên bản 3.2: Tham số tối ưu hóa.The optimize parameter.
Đã thay đổi trong phiên bản 3.4: Tiện ích mở rộng ZIP64 được bật theo mặc định.ZIP64 extensions are enabled by default.
Các trường hợp có một phương thức ngoài các đối tượng
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']8: ________ 274 [pathName, basename = '', filterfunc = none] ¶[pathname, basename='', filterfunc=None]¶
Tìm kiếm các tệp
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']75 và thêm tệp tương ứng vào kho lưu trữ.
Nếu tham số tối ưu hóa thành
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']69 không được đưa ra hoặc
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']77, tệp tương ứng là tệp
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']78, biên dịch nếu cần thiết.
Nếu tham số tối ưu hóa thành
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']69 là
$ python -m zipfile -c monty.zip spam.txt eggs.txt
7, $ python -m zipfile -c monty.zip life-of-brian_1979/
1 hoặc with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']82, chỉ các tệp có mức tối ưu hóa đó [xem
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']83] được thêm vào kho lưu trữ, biên dịch nếu cần thiết.
Nếu PathName là một tệp, tên tệp phải kết thúc bằng
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']84 và chỉ là tệp [tương ứng ____278] được thêm vào ở cấp cao nhất [không có thông tin đường dẫn]. Nếu PathName là một tệp không kết thúc bằng
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']84, thì
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile9 sẽ được nâng lên. Nếu đó là một thư mục và thư mục không phải là thư mục gói, thì tất cả các tệp
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']78 được thêm vào ở cấp cao nhất. Nếu thư mục là một thư mục gói, thì tất cả
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']78 được thêm vào tên gói dưới dạng đường dẫn tệp và nếu bất kỳ thư mục con nào là thư mục gói, tất cả đều được thêm vào theo thứ tự được sắp xếp theo thứ tự.
Basename chỉ dành cho sử dụng nội bộ.
FilterFunc, nếu được đưa ra, phải là một hàm lấy một đối số chuỗi duy nhất. Nó sẽ được thông qua mỗi đường dẫn [bao gồm mỗi đường dẫn tệp đầy đủ riêng lẻ] trước khi nó được thêm vào kho lưu trữ. Nếu FilterFunc trả về một giá trị sai, đường dẫn sẽ không được thêm vào và nếu đó là một thư mục, nội dung của nó sẽ bị bỏ qua. Ví dụ: nếu các tệp thử nghiệm của chúng tôi đều có trong các thư mục
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']90 hoặc bắt đầu với chuỗi
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']91, chúng tôi có thể sử dụng bộ lọc để loại trừ chúng:
>>> zf = PyZipFile['myprog.zip'] >>> def notests[s]: ... fn = os.path.basename[s] ... return [not [fn == 'test' or fn.startswith['test_']]] >>> zf.writepy['myprog', filterfunc=notests]
Phương thức
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']92 tạo ra tài liệu lưu trữ với tên tệp như thế này:
string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile
Mới trong phiên bản 3.4: Tham số FilterFunc.The filterfunc parameter.
Đã thay đổi trong phiên bản 3.6.2: Tham số PathName chấp nhận một đối tượng giống như đường dẫn.The pathname parameter accepts a path-like object.
Đã thay đổi trong phiên bản 3.7: Các mục nhập thư mục sắp xếp đệ quy.Recursion sorts directory entries.
Đối tượng Zipinfo
Các trường hợp của lớp
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01 được trả về bằng các phương thức with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']6 và
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']7 của các đối tượng
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']8. Mỗi đối tượng lưu trữ thông tin về một thành viên duy nhất của kho lưu trữ zip.
Có một classmethod để tạo một thể hiện
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01 cho tệp hệ thống tập tin: ClassMethod ________ 298 ________ 299 [tên tệp, arcname = none, *, strict_timestamp = true] ¶[filename, arcname=None, *,
strict_timestamps=True]¶Xây dựng một thể hiện
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
01 cho một tệp trên hệ thống tập tin, để chuẩn bị thêm nó vào tệp zip.Tên tệp phải là đường dẫn đến một tệp hoặc thư mục trên hệ thống tệp.
Nếu ArcName được chỉ định, nó được sử dụng làm tên trong kho lưu trữ. Nếu ArcName không được chỉ định, tên sẽ giống như tên tệp, nhưng với bất kỳ ký tự ổ đĩa nào và các dấu phân cách đường dẫn hàng đầu bị xóa.
Đối số StricT_Timestamp, khi được đặt thành
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]3, cho phép zip các tệp cũ hơn 1980-01-01 với chi phí đặt dấu thời gian thành 1980-01-01. Hành vi tương tự xảy ra với các tệp mới hơn 2107-12-31, dấu thời gian cũng được đặt ở giới hạn.
Mới trong phiên bản 3.6.
Đã thay đổi trong phiên bản 3.6.2: Tham số tên tệp chấp nhận một đối tượng giống như đường dẫn.The filename parameter accepts a path-like object.
Mới trong Phiên bản 3.8: Đối số chỉ từ khóa của Strict_TimestampsThe strict_timestamps keyword-only argument
Các phiên bản có các phương thức và thuộc tính sau:
________ 298 ________ 252 [][]¶Trả về
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]2 Nếu thành viên lưu trữ này là một thư mục.
Điều này sử dụng tên mục nhập tên: Các thư mục phải luôn kết thúc bằng
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']38.
Mới trong phiên bản 3.6.
Đã thay đổi trong phiên bản 3.6.2: Tham số tên tệp chấp nhận một đối tượng giống như đường dẫn.Mới trong Phiên bản 3.8: Đối số chỉ từ khóa của Strict_Timestamps
Các phiên bản có các phương thức và thuộc tính sau:________ 298 ________ 252 []
$ python -m zipfile -c monty.zip spam.txt eggs.txt
7
| ________ 298 ________ 219¶ |
$ python -m zipfile -c monty.zip life-of-brian_1979/
1
| Tên của tập tin trong kho lưu trữ. |
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']82 | ________ 298 ________ 309¶ |
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']23 | Thời gian và ngày sửa đổi cuối cùng cho thành viên lưu trữ. Đây là một bộ ba giá trị: |
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]14 | Mục lục |
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]15 | Giá trị |
Năm [> = 1980]
Tháng [dựa trên một]
Ngày trong tháng [dựa trên một]Giờ [không dựa trên]
Biên bản [không dựa trên]
Giây [dựa trên không]
Ghi chúĐịnh dạng tệp ZIP không hỗ trợ dấu thời gian trước năm 1980.
________ 298 ________ 317¶Loại nén cho thành viên lưu trữ.
Nhận xét cho thành viên lưu trữ riêng lẻ là đối tượng
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']03.Dữ liệu trường mở rộng. Ghi chú ứng dụng PKZIP chứa một số nhận xét về cấu trúc bên trong của dữ liệu có trong đối tượng
with ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']03 này.
________ 298 ________ 321¶
Hệ thống tạo ra kho lưu trữ zip.________ 298 ________ 323¶
Phiên bản PKZIP tạo ra kho lưu trữ zip.Phiên bản PKZIP cần thiết để trích xuất Lưu trữ.
________ 298 ________ 325¶Phải bằng không.
________ 298 ________ 327¶BIT cờ zip.
________ 298 ________ 329¶
Số lượng tiêu đề tập tin.________ 298 ________ 331¶
Thuộc tính nội bộ.________ 298 ________ 333¶
Thuộc tính tệp bên ngoài.Byte bù vào tiêu đề tập tin.
________ 298 ________ 335¶
CRC-32 của tệp không nén.
________ 298 ________ 337¶
$ python -m zipfile -c monty.zip spam.txt eggs.txt
Kích thước của dữ liệu nén.
$ python -m zipfile -c monty.zip life-of-brian_1979/
________ 298 ________ 339¶
$ python -m zipfile -e monty.zip target-dir/
Kích thước của tập tin không nén.
zipdir["foo"] #Just give it a dir and get a .zip file
zipdir["foo", "foo2.zip"] #Get a .zip file with a specific file name
zipdir["foo", "foo3nodir.zip", False] #Omit the top level directory
zipdir["../test1/foo", "foo4nopardirs.zip"]
0Giao diện dòng lệnh
Mô-đunwith ZipFile['spam.zip', 'w'] as myzip: myzip.write['eggs.txt']9 cung cấp giao diện dòng lệnh đơn giản để tương tác với kho lưu trữ zip.
Nếu bạn muốn tạo một kho lưu trữ zip mới, hãy chỉ định tên của nó sau tùy chọn
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]41 và sau đó liệt kê [các] tên tệp nên được bao gồm:Vượt qua một thư mục cũng được chấp nhận:
Nếu bạn muốn trích xuất một kho lưu trữ zip vào thư mục được chỉ định, hãy sử dụng tùy chọn
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]42:Đối với danh sách các tệp trong kho lưu trữ zip, hãy sử dụng tùy chọn
with ZipFile['spam.zip'] as myzip: with myzip.open['eggs.txt'] as myfile: print[myfile.read[]]43:
Tùy chọn dòng lệnh mà
________ 344 ________ 345¶ ________ 346 ________ 345¶Liệt kê các tập tin trong một zipfile.
________ 348 ________ 349¶ ________ 350 ________ 349¶
Tạo zipfile từ các tập tin nguồn.
________ 352 ________ 353¶
Trích xuất zipfile vào thư mục đích.
________ 354 ________ 345¶ ________ 356 ________ 345¶
Kiểm tra xem zipfile có hợp lệ hay không.
Giới hạn tài nguyên
Việc thiếu bộ nhớ hoặc khối lượng đĩa sẽ dẫn đến giải nén không thành công. Ví dụ, bom giải nén [còn gọi là bom zip] áp dụng cho thư viện zipfile có thể gây cạn kiệt khối lượng đĩa.
Sự gián đoạn
Sự gián đoạn trong quá trình giải nén, chẳng hạn như cấp kiểm soát-C hoặc tiêu diệt quá trình giải nén có thể dẫn đến giải nén không hoàn toàn của kho lưu trữ.