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

Python 通过SFTP从远程服务器读取GZIP文本文件,python,gzip,fabric,Python,Gzip,Fabric,我在远程服务器上有许多大型文本文件,我希望在不通过编程方式解压缩的情况下读取这些文件 我有从远程服务器读取非GZIP文本文件以及本地读取GZIP文本文件的功能。我不知道如何将两者结合起来,或者是否可能 下面是代码的各个工作部分: from contextlib import closing from fabric.network import connect from fabric import state import gzip # This successfully reads a non

我在远程服务器上有许多大型文本文件,我希望在不通过编程方式解压缩的情况下读取这些文件

我有从远程服务器读取非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.gzip文件中,如下所示:

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
适用于大型(Gzip时为626M)文本文件。非常感谢。