如何使用Python GZip模块压缩文件夹? [英] How do I compress a folder with the Python GZip module?

查看:962
本文介绍了如何使用Python GZip模块压缩文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建压缩文件/文件夹的Python软件。我如何创建一个代码段,要求用户输入文件夹位置,然后压缩它。我目前有一个单一的文件,而不是一个文件夹充满了文件的代码。

I'm creating Python software that compresses files/folders... How would I create a section of the code that asks for the user input of the folder location and then compresses it. I currently have the code for a single file but not a folder full of files. Please explain in detail how to do this.

推荐答案

压缩tar文件夹中的文件的代码是:

The code to compress a folder in to tar file is:

import tarfile

tar = tarfile.open("TarName.tar.gz", "w:gz")
tar.add("folder/location", arcname="TarName")
tar.close()

它适用于我。希望也为你工作。

It works for me. Hope that works for you too.

这篇关于如何使用Python GZip模块压缩文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆