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.

Answer from ecL3siast on Stack Overflow
🌐
GitHub
gist.github.com › kstreepy › a9800804c21367d5a8bde692318a18f5
For a given directory, unzip all .gz files in folder, save unzipped files in folder and deleted zipped files. A python solution for instances where you do not have access to PowerShell. · GitHub
For a given directory, unzip all .gz files in folder, save unzipped files in folder and deleted zipped files. A python solution for instances where you do not have access to PowerShell. - gz_extract.py
🌐
AskPython
askpython.com › python-modules › gzip-module-in-python
Python HowTo - Using the gzip Module in Python - AskPython
August 6, 2022 - Also, you would observe that the file sample.txt.gz is created on your current directory. Alright, so we’ve successfully written to this compressed file. Let’s now try to decompress it and read it’s contents. Now, similar to the write() function via the wrapper, we can also read() using that same function. import gzip import os import io name = 'sample.txt.gz' with gzip.open(name, 'rb') as ip: with io.TextIOWrapper(ip, encoding='utf-8') as decoder: # Let's read the content using read() content = decoder.read() print(content)
🌐
Python
docs.python.org › 3 › library › gzip.html
gzip — Support for gzip files
Source code: Lib/gzip.py This module provides a simple interface to compress and decompress files just like the GNU programs gzip and gunzip would. This is an optional module. If it is missing from...
🌐
Python Forum
python-forum.io › thread-38556.html
python gzip all files from a folder
Hi Team, I want to gzip all files of a folder \or complete folder. save with different name. before deleting all files from a folder. how to achieve this. with the below code I can gzip single file. import gzip import shutil fname = 'D:\bigcsv.cs...
🌐
TutorialsPoint
tutorialspoint.com › python-support-for-gzip-files-gzip
The gzip Module
June 25, 2020 - import gzip data=b'Python - Batteries included' with gzip.open("test.txt.gz", "wb") as f: f.write(data) This will create "test.txt.gz" file in current directory. This gzip archive contains "test.txt" which you can verify using any unzipping utility. To programmatically read this compressed file.
Find elsewhere
🌐
Aspose
blog.aspose.com › aspose.blogs › create and unzip gzip files in python
GZIP Files in Python | Python GZIP Module
September 11, 2023 - The following are the steps to unzip a GZIP archive in Python. First, load the GZIP file using GzipArchive class. Then, unzip GZIP file using GzipArchive.extract_to_directory() method.
🌐
Codecademy Forums
discuss.codecademy.com › community › off topic
Python gzip folder with high compress level - Off Topic - Codecademy Forums
January 9, 2018 - I want to write a python script which can compress multiple folders and their content with gzip with maximum compression ratio. As an example I have following folders in my linux directory with mentioned folder size /tmp/project1 (50MB) /tmp/project2 (100MB) I need to compress both folders together with a single name and need to reduce total compressed size at least 50% of total actual folder size.
🌐
Xah Lee
xahlee.info › python › gzip.html
Python: Compress Decompress Gzip File
March 23, 2019 - # compress a gzip file import gzip input = open("/Users/joe/xxtest", 'rb') s = input.read() input.close() output = gzip.GzipFile("/Users/joe/xxtest.gz", 'wb') output.write(s) output.close() print("done")
🌐
Python Module of the Week
pymotw.com › 2 › gzip
gzip – Read and write GNU zip files - Python Module of the Week
Now available for Python 3! Buy the book! ... The gzip module provides a file-like interface to GNU zip files, using zlib to compress and uncompress the data.
🌐
Python Pool
pythonpool.com › home › tutorials › python gzip: compress and decompress files correctly
Python gzip: Compress and Decompress Files Correctly
December 18, 2020 - According to the current Python docs, the command-line interface keeps the input file. That differs from some system gzip commands that replace the original file by default. Mixing text and binary modes. Use rt/wt for strings and rb/wb for bytes. Reading huge files at once. Stream large files with shutil.copyfileobj() instead of calling read() on the entire file. Expecting gzip to archive directories...
🌐
Medium
medium.com › @anirudhbishnoi5 › gzip-module-in-python-230aafd7d17a
gzip module in Python. gzip is an inbuilt module in python… | by Anirudh Bishnoi | Medium
April 9, 2023 - gzip is an inbuilt module in python which is used to compress and decompress files just like GNU programs gzip and gunzip would do. The data compression is provided by zlib module.
🌐
Stack Overflow
stackoverflow.com › questions › 49393114 › gzip-all-files-in-a-folder
python - GZip all files in a folder - Stack Overflow
import os import gzip import shutil extension = ".gz" dir_name = 'C:\\test' dest_name= 'C:\\test\Extract' os.chdir(dir_name) # change directory from working dir to dir with files for item in os.listdir(dir_name): # loop through items in dir if item.endswith(extension): # check for ".gz" extension file_name = os.path.abspath(item) # get full path of files with gzip.open(file_name, 'rb') as f_in: with open(file_name+ '.txt', 'wb') as f_out: shutil.copyfileobj(f_in, f_out)
🌐
Digi
docs.digi.com › resources › documentation › digidocs › 90001537 › references › r_using_zip__gzip_or_compression.htm
Using ZIP, GZIP or compression
August 23, 2018 - import gzip f_in = open('WEB/Python/file.txt', 'rb') f_out = gzip.open('WEB/Python/file.zip', 'wb') f_out.writelines(f_in) f_out.close() f_in.close()
🌐
Scaler
scaler.com › home › topics › python gzip module
Python gzip Module| Scaler Topics
December 15, 2022 - We learned so far, how the Python gzip module offers an easy command line interface where you can efficiently compress or decompress the files. The input files are kept as it is when you execute the Python gzip module. Below are the various Command Line Interface options, you can use while working with the Python gzip module.
🌐
GitHub
gist.github.com › dreikanter › 2835292
Create tar.gz archive with Python, unpack files back and check the result · GitHub
Create tar.gz archive with Python, unpack files back and check the result - gzip-demo.py