【问题标题】:How to unzip a list of files and push it to S3?如何解压缩文件列表并将其推送到 S3?
【发布时间】:2020-07-02 15:00:24
【问题描述】:

我的文件列表如下:

file1.log.gz
files2.log.gz
...

现在我想解压缩上面列表中的每个文件并将其推送到 S3。

import gzip
with gzip.open('/home/joe/file.txt.gz', 'rb') as f:
    file_content = f.read()

我不确定在我的列表中以path = /home/joe/file.txt.gz 的形式给出什么。 您能否提供一些见解?

【问题讨论】:

    标签: python gzip


    【解决方案1】:

    您需要提供所有文件所在文件夹的路径。那么

    import os
    import gzip
    
    my_folder = "path to folder here"
    for fname in os.listdir(my_folder):
       file_content = gzip.open(os.path.join(your_folder, fname), 'rb')
       #rest of your code goes here
    

    这将在 your_folder 中所有文件的类型正确的情况下起作用。如果不是,则需要根据文件后缀或其他添加条件。

    【讨论】:

      【解决方案2】:

      也许你可以试试:

      import gzip
      
      g = gzip.GzipFile(mode="rb", fileobj=open('/home/joe/file.txt.gz', 'rb'))
      with open(thePath,"wb") as f:
          f.write(g.read())
      

      More could refer to here

      【讨论】:

        猜你喜欢
        • 2016-03-31
        • 1970-01-01
        • 2021-09-12
        • 2014-07-26
        • 1970-01-01
        • 1970-01-01
        • 2014-05-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多