跳转到内容

The Wikibooks community is developing a policy on the use of generative AI. Please review the draft policy and provide feedback on its talk page.

Python 3:文件和数字资产操作/文件/压缩

来自,开放的书籍,开放的世界

文件的压缩

[编辑 | 编辑源代码]

整个目录的压缩

[编辑 | 编辑源代码]
from shutil import make_archive
def main():
    directory_to_save: str = 'C:\\Data_dir'
    output_to: str = 'C:\\Archived.zip'

    archive_location: str = make_archive(
        output_to,
        'zip',
        directory_to_save
    )


if __name__ == '__main__':
    main()

使用 ZipFile 压缩

[编辑 | 编辑源代码]
from zipfile import ZipFile
def main():
    with ZipFile('test.zip', 'w') as object:
        object.write('D:\\main.py', 'main.py')


if __name__ == '__main__':
    main()
检索自 "https://wikibooks.cn/w/index.php?title=Python_3:_working_with_files_and_digital_assets/File/Compression&oldid=4373609"
华夏公益教科书