Compress a file in memory, compute checksum and write it as `gzip` in python
问题 I want to compress files and compute the checksum of the compressed file using python. My first naive attempt was to use 2 functions: def compress_file(input_filename, output_filename): f_in = open(input_filename, 'rb') f_out = gzip.open(output_filename, 'wb') f_out.writelines(f_in) f_out.close() f_in.close() def md5sum(filename): with open(filename) as f: md5 = hashlib.md5(f.read()).hexdigest() return md5 However, it leads to the compressed file being written and then re-read. With many