Skip to content

Instantly share code, notes, and snippets.

@glwinsor
Last active August 27, 2018 20:12
Show Gist options
  • Save glwinsor/ced53ea01171c24243dc5c34e63425eb to your computer and use it in GitHub Desktop.
Save glwinsor/ced53ea01171c24243dc5c34e63425eb to your computer and use it in GitHub Desktop.
[system call and capture STDOUT]
#! /usr/bin/python3
from subprocess import Popen,PIPE,STDOUT
pmidString=''
command='/home/glwinsor/edirect/efetch -db pubmed -id 153904'
#captures STDOUT without having to use a redirect to a filename
#note that it captures bytes, not a string. Therefore need to specify universtal_newlines=True in order to retain newlines
p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,universal_newlines=True)
output = p.stdout.read()
# now write the contents of output to filename.txt
f = open('filename.txt', 'w')
f.write(str(output))
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment