samedi 31 octobre 2015

Execute zgrep command and write results to a file

I have a folder containing lots of files like file_1.gz to file_250.gz and increasing. A zgrep command which searches through them is like:

zgrep -Pi "\"name\": \"bob\"" ../../LM/DATA/file_*.gz 

I want to execute this command in a python subprocess like:

out_file = os.path.join(out_file_path, file_name)
search_command = ['zgrep', '-Pi', "\"name\": \"bob\"", "../../LM/DATA/file_*.gz "]
process = subprocess.Popen(search_command, stdout=out_file)

The problem is the out_file is created but it is empty and these errors are raised:

<type 'exceptions.AttributeError'>
'str' object has no attribute 'fileno'

What is the solution? Thanks in advanced.

Aucun commentaire:

Enregistrer un commentaire