How to tell if a file is gzip compressed?

前端 未结 6 1348
挽巷
挽巷 2021-01-03 19:21

I have a Python program which is going to take text files as input. However, some of these files may be gzip compressed.

Is there a cross-platform, usable from Py

6条回答
  •  春和景丽
    2021-01-03 19:52

    Doesn’t seem to work well in python3...

    import mimetypes
    filename = "./datasets/test"
    
    def file_type(filename):
        type = mimetypes.guess_type(filename)
        return type
    print(file_type(filename))
    

    returns (None, None) But from the unix command "File"

    :~> file datasets/test datasets/test: gzip compressed data, was "iostat_collection", from Unix, last modified: Thu Jan 29 07:09:34 2015

提交回复
热议问题