Python Forum
python gzip all files from a folder - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python gzip all files from a folder (/thread-38556.html)



python gzip all files from a folder - mg24 - Oct-28-2022

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.csv" 

with open(fname,'rb') as f_in:
with gzip.open(f'{fname}.gz','wb') as f_out:
` print(“success”)



RE: python gzip all files from a folder - ibreeden - Oct-28-2022

(Oct-28-2022, 04:16 AM)mg24 Wrote: with the below code I can gzip single file.
I think you forgot to fill the zipfile in your example. What is the size of the zipfile? I guess it is 0 bytes.

But how to zip all files in a folder. You already know a lot of how to do that. In an other thread you already had:
import os    
import glob
path = f"{Constant_folder}\{site}\{tbl}"

os.chdir(path)
files = glob.glob('*.*')
for filename in files:
    os.unlink(filename)
You can now use the same principle to zip all files instead of unlinking them.
import os    
import glob
import gzip
import shutil

path = f"{Constant_folder}/{site}/{tbl}"

os.chdir(path)
files = glob.glob('*.*')
for filename in files:
    with open(filename, "rb") as f_in:
        with gzip.open(f"{filename}.gz", "wb") as f_out:
            shutil.copyfileobj(f_in, f_out)   # You forgot this line in your example.
    print(f"success. Zipped {filename}")



RE: python gzip all files from a folder - Larz60+ - Oct-28-2022

The following code creates a .zip file, you can modify to create gzip file
if you set the suffix flag, only those files will be zipped, otherwise all files will be zipped
set attribute 'full_path_in_zip' True to keep entire path in zipfile, otherwise immediate directory only will be kept.
from pathlib import Path
import os
import zipfile


def compress_files(fpath, zipfilename, suffix=None, full_path_in_zip=False):
    if not full_path_in_zip:
        savepath = os.getcwd()
        os.chdir(fpath)
    if suffix:
        filelist = [filename.name for filename in fpath.iterdir() if filename.is_file() and filename.suffix == suffix]
    else:
        filelist = [filename.name for filename in fpath.iterdir() if filename.is_file()]


    with zipfile.ZipFile(zipfilename, mode="w") as zippy:
        for filename in filelist:
            zippy.write(filename)

    if not full_path_in_zip:
        os.chdir(savepath)

def main():
    filedir = Path('.../data/csv/')
    zipfile = Path('zippedcsv.zip')
    compress_files(filedir, zipfilename=zipfile, suffix='.csv')


if __name__ == '__main__':
    main()



RE: python gzip all files from a folder - mg24 - Oct-28-2022

Hi Larz60,

superb ! I liked your code. Thumbs Up

thanks for your help.



Thanks
mg