通过 SFTP 从远程服务器读取 GZIP 文本文件

Read GZIP text file from remote server over SFTP

我在远程服务器上有很多大型文本文件,我想在不以编程方式解压缩的情况下阅读这些文件

我具有从远程服务器读取非 GZIP 文本文件以及在本地读取 GZIP 文本文件的功能。我不确定如何将两者结合起来,或者是否可能

下面是单独工作的代码片段:

from contextlib import closing
from fabric.network import connect
from fabric import state
import gzip

# This successfully reads a non-GZIP text file from user@host:filePath
with closing(connect("user", "host", "port", None)) as ssh:
    with closing(ssh.open_sftp()) as sftp:
        with closing(sftp.open("filePath")) as f:
            for line in f:
                print line

# This successfully reads a GZIP text file locally
with gzip.open("fileName", "r") as f:
    for line in f:
        print line

但是还没有测试过,您可以将您从中获取的文件处理程序 f 传递给 gzip.GzipFile,如下所示:

with closing(connect("user", "host", "port", None)) as ssh:
    with closing(ssh.open_sftp()) as sftp:
        with closing(sftp.open("filePath")) as f:
            with gzip.GzipFile(mode='rb', fileobj=f) as fin:
                for line in fin:
                    print line