如何确定Python中压缩文件的内容长度? [英] How to determine the Content-Length of a gzipped file in Python?

查看:107
本文介绍了如何确定Python中压缩文件的内容长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的压缩文件很大,我想知道内容的大小而不解压缩.我已经尝试过了:

I've a big compressed file and I want to know the size of the content without uncompress it. I've tried this:

import gzip
import os

with gzip.open(data_file) as f:
          f.seek(0, os.SEEK_END)
          size = f.tell()

但我收到此错误

ValueError: Seek from end not supported 

我该怎么办?

谢谢.

推荐答案

要进行总结,我需要打开巨大的压缩文件(> 4GB),以便Dan的技术无法正常工作,并且我想要长度(行数)文件,因此Mark Adler的技术不合适.

To summerize, I need to open huges compressed files (> 4GB) so the technique of Dan won't work and I want the length (number of line) of the file so the technique of Mark Adler is not appropriate.

最终,我找到了针对未压缩文件的解决方案(不是最优化的,但它可以工作!),可以轻松地将其转换为压缩文件:

Eventually, I found for uncompressed files a solution( not the most optimized but it works!) which can be transposed easily to compressed files:

size = 0

with gzip.open(data_file) as f:
  for line in f:
    size+= 1
    pass

return size

谢谢大家,这个论坛的人们非常有效!

Thank you all, people in this forum are very effective!

这篇关于如何确定Python中压缩文件的内容长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆