Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách nén file Python lớn?

Dùng Python nén/giải nén file. Module zipfile nén/giải nén file đơn lẻ hoặc nhiều file cùng lúc. Quá trình dễ dàng, code rất ít. Import zipfile, mở ZipFile object ở write mode (tham số 2 là 'w'). File cần zip ở cùng folder hoặc chỉ định path. Code:

import zipfile
foo_zip = zipfile.ZipFile('foo.zip', 'w')
foo_zip.write('foo.txt', compress_type=zipfile.ZIP_DEFLATED)
foo_zip.close()

Tạo file zip foo.zip từ foo.txt.