Search code examples
pythonloopscsvgzip

Loop for converting multiple files from csv.gz to csv


I have several csv.gzip files I am trying to convert and save to csv files. I'm able to do it for an individual file using:

with gzip.open('Pool.csv.gz') as f:
    Pool= pd.read_csv(f)
Pool.to_csv("Pool.csv")

I'm trying to create a loop to convert all files in directory but I'm failing. Here is my code:

import gzip
import glob
import os
os.chdir('/home/path')
extension = 'csv.gz'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
for i in range(len(all_filenames)):  gzip.open(i) as f:
    pool_1 = pd.read_csv(f)

Solution

  • You can use os.listdir() to create your list of files and then loop through it:

    import os
    import gzip
    import pandas as pd
    
    dir_path = "/home/path"
    all_files = [f for f in os.listdir(dir_path) if f.endswith('csv.gz')]
    
    for file in all_files:
        with gzip.open(f"{dir_path}/{file}") as f:
            df = pd.read_csv(f)
            df.to_csv(f"{file.split('.')[0]}.csv")