Hướng dẫn python zip directory - thư mục zip python
Nối tiếp tutorial về lập trình Python, hôm nay chúng ta sẽ học cách tạo ra file zip bằng cách sử dụng Python, thông qua thư viện zipfile. Thư viện này là build-in của Python 3 nên các bạn không cần phải thực hiện cài đặt. Show Tạo File Zip sử dụng PythonĐầu tiên là import thư viện filezip import zipfile Lựa chọn mode để nén file zip # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) Tạo file Zip với đường dẫn và mode (w – write, a – append) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') Thêm file vào file zip vừa tạo # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression Close file # Don't forget to close the file! zf.close() Vậy đoạn code đầy đủ sẽ là: import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close() Kết quả Sau khi chạy đoạn code trên chúng ta đã tạo thành công file zip Đây là chương trình tạo file zip đơn giản, các bạn muốn làm nhiều thư hơn như zip folder, tạo mật khẩu cho file zip thì vui lòng tham khảo hướng dẫn sử dụng thư viện zipfile ở đây. Nguồn vinasupport.com Đã hỏi 12 năm, 10 tháng trước 12 years, 10 months ago Đã xem 628k lần 628k times 777 Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm. Làm thế nào tôi có thể tạo một kho lưu trữ zip của một cấu trúc thư mục trong Python?
Tomerikoo 16.7K15 Huy hiệu vàng38 Huy hiệu bạc55 Huy hiệu Đồng15 gold badges38 silver badges55 bronze badges Đã hỏi ngày 6 tháng 12 năm 2009 lúc 11:12Dec 6, 2009 at 11:12
4 Cách dễ nhất là sử dụng # Don't forget to close the file! zf.close()5. Nó hỗ trợ cả định dạng zip và tar.
Nếu bạn cần làm một cái gì đó phức tạp hơn so với việc hạ gục toàn bộ thư mục (chẳng hạn như bỏ qua một số tệp nhất định), thì bạn sẽ cần đào vào mô -đun # Don't forget to close the file! zf.close()6 như những người khác đã đề xuất.
Phượng Hoàng 6.6184 Huy hiệu vàng37 Huy hiệu bạc44 Huy hiệu đồng4 gold badges37 silver badges44 bronze badges Đã trả lời ngày 3 tháng 9 năm 2014 lúc 17:26Sep 3, 2014 at 17:26
Crdaviscrdaviscrdavis 19.3k3 Huy hiệu vàng13 Huy hiệu bạc9 Huy hiệu đồng3 gold badges13 silver badges9 bronze badges 22 Như những người khác đã chỉ ra, bạn nên sử dụng zipfile. Tài liệu cho bạn biết những chức năng nào có sẵn, nhưng không thực sự giải thích cách bạn có thể sử dụng chúng để zip toàn bộ thư mục. Tôi nghĩ rằng dễ dàng nhất để giải thích với một số mã ví dụ:
Nico Schlömer 49.1K25 Huy hiệu vàng186 Huy hiệu bạc226 Huy hiệu Đồng25 gold badges186 silver badges226 bronze badges Đã trả lời ngày 6 tháng 12 năm 2009 lúc 11:23Dec 6, 2009 at 11:23
Mark Byersmark ByersMark Byers 784K188 Huy hiệu vàng1552 Huy hiệu bạc1440 Huy hiệu đồng188 gold badges1552 silver badges1440 bronze badges 15 Để thêm nội dung của # Don't forget to close the file! zf.close()7 vào tệp zip mới, bao gồm tất cả các tệp và thư mục con:
Đã trả lời ngày 6 tháng 12 năm 2009 lúc 11:28Dec 6, 2009 at 11:28
Ben Jamesben JamesBen James Huy hiệu vàng 117K2626 gold badges191 silver badges155 bronze badges 5
TomerikooTrong Python 2.7+, # Don't forget to close the file! zf.close()8 có chức năng # Don't forget to close the file! zf.close()9.
Ở đây, kho lưu trữ bị kéo dài sẽ được đặt tên là import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close()0. Nếu import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close()1 xa hơn so với import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close()2, nó sẽ loại trừ các tệp không có trong import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close()1, nhưng vẫn lưu trữ các tệp trong cha mẹ cho đến import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close()2. Tôi đã có một vấn đề kiểm tra vấn đề này trên Cygwin với 2.7 - nó muốn một đối số root_dir, cho CWD: # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex)0 Sử dụng Python từ vỏBạn có thể làm điều này với Python từ vỏ cũng bằng cách sử dụng mô -đun # Don't forget to close the file! zf.close()6: # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex)1 Trong đó import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close()6 là tên của tệp đích bạn muốn (thêm import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close()7 nếu bạn muốn, nó sẽ không tự động thực hiện nó) và SourcedIR là đường dẫn đến thư mục. Zipping Up Python (hoặc không chỉ muốn cha mẹ):Nếu bạn đang cố gắng tăng cường gói Python với import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close()8 và import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close()9 và bạn không muốn cha mẹ, thì đó là # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex)2 Và # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex)3 Sẽ chạy gói. (Lưu ý rằng bạn không thể chạy các gói con dưới dạng điểm nhập từ kho lưu trữ có khóa.) Zipping a Python Ứng dụng:Nếu bạn có Python3.5+và đặc biệt muốn tăng cường gói Python, hãy sử dụng ZipApp: # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex)4 Đã trả lời ngày 31 tháng 3 năm 2016 lúc 18:57Mar 31, 2016 at 18:57
0 Hàm này sẽ đệ quy để tăng cường một cây thư mục, nén các tệp và ghi các tên tệp tương đối chính xác trong kho lưu trữ. Các mục lưu trữ giống như các mục được tạo bởi 0.# Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex)5
Đã trả lời ngày 13 tháng 6 năm 2013 lúc 6:57Jun 13, 2013 at 6:57
George V. Reillygeorge V. ReillyGeorge V. Reilly 15.3k7 Huy hiệu vàng41 Huy hiệu bạc38 Huy hiệu đồng7 gold badges41 silver badges38 bronze badges 1 Sử dụng SOWLIL, là một phần của bộ thư viện tiêu chuẩn Python. Sử dụng SHOTIL rất đơn giản (xem mã bên dưới):
Code: # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex)6
Qback 3.7352 Huy hiệu vàng22 Huy hiệu bạc35 Huy hiệu Đồng2 gold badges22 silver badges35 bronze badges Đã trả lời ngày 12 tháng 10 năm 2018 lúc 9:46Oct 12, 2018 at 9:46
Vadiraj s jvadiraj s jVadiraj S J 6119 Huy hiệu bạc17 Huy hiệu đồng9 silver badges17 bronze badges 2 Python hiện đại (3.6+) sử dụng mô-đun 1 để xử lý các đường dẫn giống như OOP và 2 để làm toàn cầu đệ quy. Theo như tôi có thể nói, điều này tương đương với câu trả lời của George V. Reilly: Zips với nén, phần tử hàng đầu là một thư mục, giữ cho các dirs trống, sử dụng các đường dẫn tương đối.# Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex)7 Lưu ý: Như các gợi ý loại tùy chọn chỉ ra, 3 không thể là đối tượng đường dẫn (sẽ được sửa trong 3.6.2+).Đã trả lời ngày 31 tháng 3 năm 2017 lúc 13:01Mar 31, 2017 at 13:01
monk-timemonk-timemonk-time 1.87212 Huy hiệu bạc17 Huy hiệu đồng12 silver badges17 bronze badges 1 Với mô -đun Python 3.9, 1 & # Don't forget to close the file! zf.close()6, bạn có thể tạo một tệp zip từ bất cứ đâu trong hệ thống. # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex)8 Nó là gọn gàng, gõ và có ít mã hơn. Đã trả lời ngày 17 tháng 8 năm 2021 lúc 12:05Aug 17, 2021 at 12:05
JD Solankijd SolankiJD Solanki 7106 Huy hiệu bạc15 Huy hiệu Đồng6 silver badges15 bronze badges Để thêm nén vào tệp zip kết quả, hãy xem liên kết này. Bạn cần phải thay đổi: # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex)9 đến # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')0
Philshem 24.2K7 Huy hiệu vàng59 Huy hiệu bạc123 Huy hiệu đồng7 gold badges59 silver badges123 bronze badges Đã trả lời ngày 27 tháng 4 năm 2013 lúc 17:14Apr 27, 2013 at 17:14
E Smithe SmithE Smith 1371 Huy hiệu bạc3 Huy hiệu đồng1 silver badge3 bronze badges 0 Tôi đã thực hiện một số thay đổi đối với mã được đưa ra bởi Mark Byers. Chức năng dưới đây cũng sẽ thêm các thư mục trống nếu bạn có chúng. Các ví dụ sẽ làm cho nó rõ ràng hơn đường dẫn được thêm vào zip. # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')1 Trên đây là một chức năng đơn giản nên làm việc cho các trường hợp đơn giản. Bạn có thể tìm thấy lớp thanh lịch hơn trong ý chính của tôi: https://gist.github.com/eccenux/17526123107CA0AC28E6
Đã trả lời ngày 10 tháng 6 năm 2013 lúc 9:28Jun 10, 2013 at 9:28
NuxnuxNux 8.4835 Huy hiệu vàng55 Huy hiệu bạc69 Huy hiệu Đồng5 gold badges55 silver badges69 bronze badges 3 Tôi có một ví dụ mã khác có thể giúp, sử dụng python3, pathlib và zipfile. Nó sẽ hoạt động trong bất kỳ hệ điều hành nào. # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')2 Đã trả lời ngày 2 tháng 10 năm 2015 lúc 10:03Oct 2, 2015 at 10:03
Để giữ lại hệ thống phân cấp thư mục theo thư mục phụ huynh để được lưu trữ: # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')3 Nếu bạn muốn, bạn có thể thay đổi điều này để sử dụng 1 để tìm thấy tệp.Đã trả lời ngày 9 tháng 7 năm 2019 lúc 8:48Jul 9, 2019 at 8:48
Ryanjdillonryanjdillonryanjdillon 16.5k9 Huy hiệu vàng82 Huy hiệu bạc104 Huy hiệu đồng9 gold badges82 silver badges104 bronze badges Nếu bạn muốn có một chức năng như thư mục nén của bất kỳ trình quản lý tệp đồ họa phổ biến nào bạn có thể sử dụng mã sau, nó sẽ sử dụng mô -đun ZipFile. Sử dụng mã này, bạn sẽ có tệp zip với đường dẫn làm thư mục gốc của nó. # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')4 Đã trả lời ngày 21 tháng 3 năm 2016 lúc 13:40Mar 21, 2016 at 13:40
VGe0rgeVGe0rgeVGe0rge 1.0103 huy hiệu vàng15 Huy hiệu bạc18 Huy hiệu đồng3 gold badges15 silver badges18 bronze badges Vì vậy, nhiều câu trả lời ở đây và tôi hy vọng tôi có thể đóng góp với phiên bản của riêng mình, dựa trên câu trả lời ban đầu (nhân tiện), nhưng với góc độ đồ họa hơn, cũng sử dụng bối cảnh cho mỗi thiết lập # Don't forget to close the file! zf.close()6 và sắp xếp 8 có một đầu ra đặt hàng.Có các thư mục này và các tệp chúng (trong số các thư mục khác), tôi muốn tạo một import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close()7 cho mỗi thư mục 0:# create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')5 Đây là những gì tôi đã áp dụng, với các nhận xét để hiểu rõ hơn về quy trình. # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')6 Về cơ bản, đối với mỗi lần lặp qua 1, tôi đang mở bối cảnh cho thiết lập # Don't forget to close the file! zf.close()6 và sau đó, lặp lại trên 3, đây là một ____74 của các tệp từ thư mục 5, hình thành đường dẫn tương đối cho mỗi tệp dựa trên thư mục 5 hiện tại đến bối cảnh # Don't forget to close the file! zf.close()6 đang chạy. Và đầu ra được trình bày như thế này: # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')7 Để xem nội dung của mỗi thư mục import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print(ex) # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w') # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression) # Don't forget to close the file! zf.close()7, bạn có thể sử dụng lệnh 9:# create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')8 Đã trả lời ngày 5 tháng 9 năm 2019 lúc 13:10Sep 5, 2019 at 13:10
Ivanleonczivanleonczivanleoncz 8.0974 Huy hiệu vàng53 Huy hiệu bạc48 Huy hiệu đồng4 gold badges53 silver badges48 bronze badges Bạn có thể muốn xem mô -đun # Don't forget to close the file! zf.close()6; Có tài liệu tại http://docs.python.org/l Library/zipfile.html. Bạn cũng có thể muốn 8 lập chỉ mục cấu trúc thư mục.Đã trả lời ngày 6 tháng 12 năm 2009 lúc 11:17Dec 6, 2009 at 11:17
me_andme_andme_and 14.8K7 Huy hiệu vàng61 Huy hiệu bạc96 Huy hiệu Đồng7 gold badges61 silver badges96 bronze badges Dưới đây là một biến thể của câu trả lời được đưa ra bởi Nux phù hợp với tôi: # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile('vinasupport.com.zip', mode='w')9 Đã trả lời ngày 30 tháng 7 năm 2014 lúc 23:13Jul 30, 2014 at 23:13
M Katzm KatzM Katz 4.8723 Huy hiệu vàng41 Huy hiệu bạc64 Huy hiệu Đồng3 gold badges41 silver badges64 bronze badges Hãy thử cái dưới đây .it đã làm việc cho tôi.. # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression0 Đã trả lời ngày 23 tháng 4 năm 2015 lúc 11:18Apr 23, 2015 at 11:18
ChandrachandraChandra 1011 huy hiệu vàng3 Huy hiệu bạc12 Huy hiệu đồng1 gold badge3 silver badges12 bronze badges Để cho linh hoạt hơn, ví dụ: Chọn Thư mục/Tệp theo tên Sử dụng: # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression1 Cho một cây tập tin: # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression2 Bạn có thể, ví dụ: Chỉ chọn 2 và 3:# Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression3 Hoặc chỉ 4 trong thư mục gọi tập lệnh và thêm mọi thứ từ đó:# Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression4 Đã trả lời ngày 25 tháng 9 năm 2018 lúc 13:32Sep 25, 2018 at 13:32
pbnpbnpbn 2.2161 Huy hiệu vàng21 Huy hiệu bạc37 Huy hiệu đồng1 gold badge21 silver badges37 bronze badges 1 Giả sử bạn muốn zip tất cả các thư mục (thư mục phụ) trong thư mục hiện tại. # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression5
Uli Köhler 12.6K15 Huy hiệu vàng65 Huy hiệu bạc114 Huy hiệu đồng15 gold badges65 silver badges114 bronze badges Đã trả lời ngày 9 tháng 4 năm 2019 lúc 8:25Apr 9, 2019 at 8:25
SushilinuxsushilinuxSushilinux 7007 Huy hiệu bạc21 Huy hiệu Đồng7 silver badges21 bronze badges Zip một tệp hoặc một cây (một thư mục và các hướng phụ của nó). # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression6 Để nối vào một kho lưu trữ hiện có, Pass 5, để tạo một kho lưu trữ mới 6 (mặc định ở trên). Vì vậy, giả sử bạn muốn gói 3 cây thư mục khác nhau trong cùng một kho lưu trữ.# Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression7 Đã trả lời ngày 17 tháng 9 năm 2020 lúc 5:50Sep 17, 2020 at 5:50
Michael Ekokamichael EkokaMichael Ekoka 18.3k10 Huy hiệu vàng74 Huy hiệu bạc78 Huy hiệu đồng10 gold badges74 silver badges78 bronze badges Một giải pháp sử dụng 7, độc lập với HĐH được sử dụng:# Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression8 Đã trả lời ngày 5 tháng 1 năm 2021 lúc 9:09Jan 5, 2021 at 9:09
AlexalexAlex 2.1962 Huy hiệu vàng26 Huy hiệu bạc39 Huy hiệu Đồng2 gold badges26 silver badges39 bronze badges Cách rõ ràng để đi là đi với Shutil, như câu trả lời hàng đầu thứ hai đã nói như vậy, nhưng nếu bạn vẫn muốn đi với Zipfile vì một số lý do, và nếu bạn gặp một số rắc rối khi làm điều đó (như ERR 13 trong Windows, v.v.) , Bạn có thể sử dụng bản sửa lỗi này: # Add file to the zip file # first parameter file to zip, second filename in zip zf.write('vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression9 Cái này lặp đi lặp lại một cách đệ quy thông qua mọi bộ lọc/tệp phụ trong thư mục đã cho của bạn và ghi chúng vào tệp zip thay vì cố gắng trực tiếp zip một thư mục. Đã trả lời ngày 10 tháng 3 năm 2021 lúc 5:09Mar 10, 2021 at 5:09
Đây là một cách tiếp cận hiện đại, sử dụng pathlib và người quản lý bối cảnh. Đặt các tệp trực tiếp vào zip, thay vì trong một thư mục con. # Don't forget to close the file! zf.close()0 Đã trả lời ngày 14 tháng 12 năm 2016 lúc 21:50Dec 14, 2016 at 21:50
Rùa là những con cuteturtles rất dễ thươngTurtles Are Cute 2.9666 huy hiệu vàng28 Huy hiệu bạc37 Huy hiệu đồng6 gold badges28 silver badges37 bronze badges Tôi đã chuẩn bị một chức năng bằng cách hợp nhất giải pháp của Mark Byers với các bình luận của Reimund và Morten Zilmer (đường dẫn tương đối và bao gồm các thư mục trống). Như một thông lệ tốt nhất, 8 được sử dụng trong việc xây dựng tệp của Zipfile.Hàm cũng chuẩn bị một tên tệp zip mặc định với tên thư mục bị nén và tiện ích mở rộng '.zip'. Do đó, nó chỉ hoạt động với một đối số: thư mục nguồn được nén. # Don't forget to close the file! zf.close()1 Đã trả lời ngày 24 tháng 12 năm 2016 lúc 20:32Dec 24, 2016 at 20:32
Gürol Canbekgürol CanbekGürol Canbek 1,0081 Huy hiệu vàng14 Huy hiệu bạc20 Huy hiệu Đồng1 gold badge14 silver badges20 bronze badges # Don't forget to close the file! zf.close()2 Đã trả lời ngày 15 tháng 6 năm 2017 lúc 6:24Jun 15, 2017 at 6:24
Chà, sau khi đọc các đề xuất, tôi đã đưa ra một cách rất giống nhau hoạt động với 2.7.x mà không tạo tên thư mục "hài hước" (tên giống như tuyệt đối) và sẽ chỉ tạo thư mục được chỉ định bên trong zip. Hoặc chỉ trong trường hợp bạn cần zip của mình để chứa một thư mục bên trong với nội dung của thư mục đã chọn. # Don't forget to close the file! zf.close()3 Đã trả lời ngày 15 tháng 8 năm 2018 lúc 21:27Aug 15, 2018 at 21:27
XedretxedretXedret 1.77317 Huy hiệu bạc24 Huy hiệu đồng17 silver badges24 bronze badges Chức năng để tạo tệp zip. # Don't forget to close the file! zf.close()4 Đã trả lời ngày 19 tháng 11 năm 2018 lúc 3:55Nov 19, 2018 at 3:55
Sushhsushhsushh Huy hiệu bạc 11511 silver badge10 bronze badges 2 |