Python Forum
python gzip all files from a folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python gzip all files from a folder
#1
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”)
Reply
#2
(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}")
Reply
#3
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()
Reply
#4
Hi Larz60,

superb ! I liked your code. Thumbs Up

thanks for your help.



Thanks
mg
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to add Python folder in Windows Registry ? Touktouk 1 265 Feb-20-2024, 01:04 PM
Last Post: DeaD_EyE
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 545 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 740 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,488 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  How to loop through all excel files and sheets in folder jadelola 1 4,490 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  delete all files and subdirectory from a main folder mg24 7 1,599 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  Merge all json files in folder after filtering deneme2 10 2,346 Sep-18-2022, 10:32 AM
Last Post: deneme2
  Make a python folder .exe Extra 0 1,029 Jun-10-2022, 01:20 AM
Last Post: Extra
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,485 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  pyspark creating temp files in /tmp folder aliyesami 1 4,991 Oct-16-2021, 05:15 PM
Last Post: aliyesami

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020