Jump to content

Python 3: working with files and digital assets/File/Compression

From Wikibooks, open books for an open world

Compression of files

[edit | edit source]

Compression of entire directories

[edit | edit source]
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()

Compression with ZipFile

[edit | edit source]
from zipfile import ZipFile
def main():
    with ZipFile('test.zip', 'w') as object:
        object.write('D:\\main.py', 'main.py')


if __name__ == '__main__':
    main()