pythongzipfabric

gzip python module is giving empty bytes


I copied one .gz file to a remote machine using fabric. When I tried to read the same remote file, empty bytes are getting displayed.

Below is the python code I used to read the zip file from remote machine.

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()

It is giving below O/P:

b''

I verified on the remote machine and the content is present in the file.

Can some one please let me know how to fix this issue.


Solution

  • fabric_connect writes to fd, leaving the file pointer at end of file. You need to rewind to the front of your BytesIO file before handing it to GzipFile.

    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()