I am running the following version of Python:
$ /usr/bin/env python --version
Python 2.5.2
I am running the following Python code to write data from a child subprocess to standard output, and reading that into a Python variable called metadata
:
# Extract metadata (snippet from extractMetadata.py)
inFileAsGzip = "%s.gz" % inFile
if os.path.exists(inFileAsGzip):
os.remove(inFileAsGzip)
os.symlink(inFile, inFileAsGzip)
extractMetadataCommand = "bgzip -c -d -b 0 -s %s %s" % (metadataRequiredFileSize, inFileAsGzip)
metadataPipes = subprocess.Popen(extractMetadataCommand, stdin=None, stdout=subprocess.PIPE, shell=True, close_fds=True)
metadata = metadataPipes.communicate()[0]
metadataPipes.stdout.close()
os.remove(inFileAsGzip)
print metadata
The use case is as follows, to pull the first ten lines of standard output from the aforementioned code snippet:
$ extractMetadata.py | head
The error will appear if I pipe into head, awk, grep, etc.
The script ends with the following error:
close failed: [Errno 32] Broken pipe
I would have thought closing the pipes would be sufficient, but obviously that's not the case.