How can I write this file in Python, preferably using gzip, to a zipped file?

66 Views Asked by At

I have some code writing output to a file that I want zipped, but I can't figure out how I would write it to a zipped file.

subprocess.run([f"grep -i -m 1 'REMARK VINA RESULT:' ./output/{docking_type}/output_{filename} \
                            | awk '{{print $4}}' >> results_{rank}.txt; echo {filename} \
                            >> results_{rank}.txt"], shell=True)


At this point I can only see writing the output then taking that file and zipping it, but I'm hoping to combine those steps, since I am writing a very large number of files. From the gzip documentation this would be done via:

import gzip
content = b"Lots of content here"
with gzip.open('/home/joe/file.txt.gz', 'wb') as f:
    f.write(content)

Am I just misunderstanding gzip? Thanks for any help!

I've tried a few variations without success so far!

2

There are 2 best solutions below

0
jjislam On

Try this:

import gzip
content = b"Lots of content here"
with gzip.open('/home/joe/file.txt.gz', 'wb') as f:
    f.write(content.encode()) # encoded
0
Sanjug Sonowal On

Yes, you can write the output directly to a gzipped file.

Here's How you can achieve this.

import subprocess
import gzip

# run the shell command to get the result and filename
result = subprocess.run([f"grep -i -m 1 'REMARK VINA RESULT:' ./output/{docking_type}/output_{filename} \
                      | awk '{{print $4}}' && echo {filename}"], shell=True, stdout=subprocess.PIPE)

# convert the output to bytes
content = result.stdout

# write the content to a gzipped file
  with gzip.open(f'results_{rank}.txt.gz', 'wb') as f:
  f.write(content)

This will run the shell command and store the output in result.stdout, then write that output directly to a gzipped file with .gz extension.