我使用fabric将一个.gz文件复制到远程机器上。当我尝试读取同一个远程文件时,会显示空字节。
下面是我用来从远程机器读取zip文件的python代码。
try:
fabric_connection = Connection(host = host_name, connect_kwargs = {'key_filename' : tmp_id, 'timeout' : 10})
fd = io.BytesIO()
remote_file = '/home/test/' + 'test.txt.gz'
fabric_connection.get(remote_file, fd)
with gzip.GzipFile(mode = 'rb', fileobj = fd) as fin:
content = fin.read()
print(content)
decoded_content = content.decode()
print(decoded_content)
except BaseException as err:
assert not err
finally:
fabric_connection.close()
它给出了以下O/p:
b''
我在远程计算机上进行了验证,并且文件中存在内容。
有人能告诉我如何解决这个问题吗。
fabric_connect
写入fd
,将文件指针留在文件末尾。在将文件交给GzipFile
之前,您需要倒带到BytesIO
文件的前面。
try:
fabric_connection = Connection(host = host_name, connect_kwargs = {'key_filename' : tmp_id, 'timeout' : 10})
fd = io.BytesIO()
remote_file = '/home/test/' + 'test.txt.gz'
fabric_connection.get(remote_file, fd)
fd.seek(0)
with gzip.GzipFile(mode = 'rb', fileobj = fd) as fin:
content = fin.read()
print(content)
decoded_content = content.decode()
print(decoded_content)
except BaseException as err:
assert not err
finally:
fabric_connection.close()