Hướng dẫn python zipfile write without path - python zipfile ghi mà không có đường dẫn

Tôi có hai tập tin trong hai thư mục khác nhau, một là ____77, bản còn lại là '/home/text/second/second.pdf'. Tôi sử dụng mã sau để nén chúng:

import zipfile, StringIO
buffer = StringIO.StringIO()
first_path = '/home/test/first/first.pdf'
second_path = '/home/text/second/second.pdf'
zip = zipfile.ZipFile(buffer, 'w')
zip.write(first_path)
zip.write(second_path)
zip.close()

Sau khi tôi mở tệp zip mà tôi đã tạo, tôi có một thư mục home trong đó, sau đó có hai bộ phụ trong đó,

from os.path import basename
...
zip.write(first_path, basename(first_path))
zip.write(second_path, basename(second_path))
zip.close()
0 và
from os.path import basename
...
zip.write(first_path, basename(first_path))
zip.write(second_path, basename(second_path))
zip.close()
1, sau đó các tệp PDF. Tôi không biết làm thế nào để chỉ bao gồm hai tệp PDF thay vì có đường dẫn đầy đủ vào kho lưu trữ zip. Tôi hy vọng tôi làm cho câu hỏi của tôi rõ ràng, xin vui lòng giúp đỡ.

Hướng dẫn python zipfile write without path - python zipfile ghi mà không có đường dẫn

Dharman ♦

28.3K21 Huy hiệu vàng75 Huy hiệu bạc128 Huy hiệu đồng21 gold badges75 silver badges128 bronze badges

Hỏi ngày 18 tháng 4 năm 2013 lúc 19:54Apr 18, 2013 at 19:54

Hướng dẫn python zipfile write without path - python zipfile ghi mà không có đường dẫn

Phương thức ZipFile Write () hỗ trợ một đối số bổ sung (ArcName) là tên lưu trữ được lưu trữ trong tệp zip, vì vậy bạn chỉ cần thay đổi mã của mình bằng:

from os.path import basename
...
zip.write(first_path, basename(first_path))
zip.write(second_path, basename(second_path))
zip.close()

Khi bạn có một số thời gian rảnh rỗi khi đọc tài liệu cho Zipfile sẽ hữu ích.

Đã trả lời ngày 19 tháng 4 năm 2013 lúc 12:31Apr 19, 2013 at 12:31

João Pintojoão PintoJoão Pinto

5.2415 Huy hiệu vàng20 Huy hiệu bạc34 Huy hiệu đồng5 gold badges20 silver badges34 bronze badges

2

Tôi sử dụng chức năng này để zip một thư mục mà không bao gồm đường dẫn tuyệt đối

import zipfile
import os 
def zipDir(dirPath, zipPath):
    zipf = zipfile.ZipFile(zipPath , mode='w')
    lenDirPath = len(dirPath)
    for root, _ , files in os.walk(dirPath):
        for file in files:
            filePath = os.path.join(root, file)
            zipf.write(filePath , filePath[lenDirPath :] )
    zipf.close()
#end zipDir

Đã trả lời ngày 2 tháng 3 năm 2016 lúc 14:09Mar 2, 2016 at 14:09

Himnabilhimnabilhimnabil

3082 Huy hiệu bạc8 Huy hiệu Đồng2 silver badges8 bronze badges

Tôi nghi ngờ có thể có một giải pháp thanh lịch hơn, nhưng cái này sẽ hoạt động:

def add_zip_flat(zip, filename):
    dir, base_filename = os.path.split(filename)
    os.chdir(dir)
    zip.write(base_filename)

zip = zipfile.ZipFile(buffer, 'w')
add_zip_flat(zip, first_path)
add_zip_flat(zip, second_path)
zip.close()

Đã trả lời ngày 18 tháng 4 năm 2013 lúc 20:02Apr 18, 2013 at 20:02

Hướng dẫn python zipfile write without path - python zipfile ghi mà không có đường dẫn

shx2shx2shx2

59.4K11 Huy hiệu vàng122 Huy hiệu bạc148 Huy hiệu đồng11 gold badges122 silver badges148 bronze badges

0

Bạn có thể ghi đè tên tệp trong kho lưu trữ với tham số

from os.path import basename
...
zip.write(first_path, basename(first_path))
zip.write(second_path, basename(second_path))
zip.close()
2:

with zipfile.ZipFile(file="sample.zip", mode="w", compression=zipfile.ZIP_DEFLATED) as out_zip:
for f in Path.home().glob("**/*.txt"):
    out_zip.write(f, arcname=f.name)

Tài liệu tham khảo tài liệu: https://docs.python.org/3/l Library/zipfile.html#zipfile.zipfile.write

Đã trả lời ngày 11 tháng 5 năm 2020 lúc 7:48May 11, 2020 at 7:48

GutsgutsGuts

8427 Huy hiệu bạc9 Huy hiệu đồng7 silver badges9 bronze badges

0

Cũng có thể được thực hiện theo cách đó (điều này cho phép tạo tài liệu lưu trữ> 2GB)

import os, zipfile
def zipdir(path, ziph):
    """zipper"""
    for root, _, files in os.walk(path):
        for file_found in files:
            abs_path = root+'/'+file_found
            ziph.write(abs_path, file_found)
zipf = zipfile.ZipFile(DEST_FILE.zip, 'w', zipfile.ZIP_DEFLATED, allowZip64=True)
zipdir(SOURCE_DIR, zipf)
zipf.close()

Đã trả lời ngày 17 tháng 10 năm 2019 lúc 13:01Oct 17, 2019 at 13:01

Như João Pinto đã nói, lập luận

from os.path import basename
...
zip.write(first_path, basename(first_path))
zip.write(second_path, basename(second_path))
zip.close()
2 của zipfile.write là những gì bạn cần. Ngoài ra, đọc tài liệu của Pathlib là hữu ích. Bạn có thể dễ dàng đưa đường dẫn tương đối đến một cái gì đó với
from os.path import basename
...
zip.write(first_path, basename(first_path))
zip.write(second_path, basename(second_path))
zip.close()
4, không cần phải chuyển sang
from os.path import basename
...
zip.write(first_path, basename(first_path))
zip.write(second_path, basename(second_path))
zip.close()
5.

import zipfile
from pathlib import Path

folder_to_compress = Path("/path/to/folder")
path_to_archive = Path("/path/to/archive.zip")

with zipfile.ZipFile(
        path_to_archive,
        mode="w",
        compression=zipfile.ZIP_DEFLATED,
        compresslevel=7,
    ) as zip:
    for file in folder_to_compress.rglob("*"):
        relative_path = file.relative_to(folder_to_compress)
        print(f"Packing {file} as {relative_path}")
        zip.write(file, arcname=relative_path)

Đã trả lời ngày 1 tháng 1 lúc 16:48Jan 1 at 16:48

ZababazababaZababa

2.1203 Huy hiệu vàng13 Huy hiệu bạc25 Huy hiệu Đồng3 gold badges13 silver badges25 bronze badges