'how to check if a file is a .gz file in Python

I am working on python input-output and was given a CSV file(possible gzipped) . If it is gzipped, I have to decompress it, and then read it.

I was trying to read the first two bytes do like this:

def func(filename):
    fi = open(filenam,"rb")
    byte1 = fi.read(1)
    byte2 = fi.read(1)

then I will check byte1 and byte2 to see if they are equal to 0x1f and 0x8b, then decompress it then print every line of it.

But when I run it, I got this error:

TypeError: 'NoneType' object is not iterable

I'm new to python, can anyone help?



Solution 1:[1]

you need to use endwith() in Python to check whether a folder has .gz extension file then use gzip module to decompress it and read .gz contents

import os
import gzip
for file in os.listdir(r"C:\Directory_name"):
    if file.endswith(".gz"):
        print file
        os.chdir(r"C:\Directory_name")
        f = gzip.open(file, 'rb')
        file_content = f.read()
        f.close()

so here "file_content" variable will hold the data of your csv gzipped file

Solution 2:[2]

Understanding from what you said in the comment - "that's all I have in the function" I would assume the issue is that the function has no return value. So probably the caller of the function tries to run on the result of a function call with no return value, i.e NoneType.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 kipodi