python compress_decompress.py
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python compress_decompress.py相关的知识,希望对你有一定的参考价值。
import shutil
# import tarfile, zipfile, gzip, bz2
#
# shutil == higher level util
# unpack
shutil.unpack_archive('archive_name.tgz')
# pack
'''
Supports:
- zip
- tar
- bztar
- gztar
To get supported archive formats:
shutil.get_archie_formats()
'''
shutil.make_archive('base_name', 'zip')
# get list of supported formats(second param of pack)
shutil.get_archive_formats()
# ==============================================================================
# Shutil make_archive
# ==============================================================================
# https://docs.python.org/2/library/shutil.html#shutil.make_archive
from shutil import make_archive
import os
archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
root_dir = os.path.expanduser(os.path.join('~', '.shh'))
make_archive(archive_name, 'gztar', root_dir)
# results in
# '/Users/tarek/myarchive.tar.gz'
# ==============================================================================
# Compressing a directory
# ==============================================================================
# https://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory
import os
import zipfile
def zipdir(path, ziph):
for rootdir, subdirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(rootdir, file))
if __name__ == '__main__':
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
以上是关于python compress_decompress.py的主要内容,如果未能解决你的问题,请参考以下文章
Python代写,Python作业代写,代写Python,代做Python
Python开发
Python,python,python
Python 介绍
Python学习之认识python
python初识