我有两个不同目录下的文件,一个是'/home/test/first/first.pdf'
,另一个是'/home/text/second/second.pdf'
。我使用以下代码来压缩它们:
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()
当我打开我创建的zip文件时,其中有一个home
文件夹,在里面有两个子文件夹first
和second
,以及pdf文件。我不知道如何只包括两个pdf文件而不是将完整路径压缩到zip归档中。我希望我的问题清楚明了,请帮忙。
pathlib
处理文件路径,您可以使用first_path.name
。https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.name - rubick