Reputation: 310
I am using a python package https://pypi.python.org/pypi/mygene to do some gene id mapping. I have code below:
#! /usr/bin/env python
# ID mapping using mygene
# https://pypi.python.org/pypi/mygene
# http://nbviewer.ipython.org/gist/newgene/6771106
# http://mygene-py.readthedocs.org/en/latest/
# 08/30/14
__author__ = 'tommy'
import mygene
import fileinput
import sys
mg = mygene.MyGeneInfo()
# mapping gene symbols to Entrez gene ids and Ensemble gene ids.
# fileinput will loop through all the lines in the input specified as file names given in command-line arguments,
# or the standard input if no arguments are provided.
# build a list from an input file with one gene name in each line
def get_gene_symbols():
gene_symbols = []
for line in fileinput.input():
gene_symbol = line.strip() # assume each line contains only one gene symbol
gene_symbols.append(gene_symbol)
fileinput.close()
return gene_symbols
Entrez_ids = mg.querymany(get_gene_symbols(), scopes='symbol', fields='entrezgene', species='human', as_dataframe=True)
# set as_dataframe to True will return a pandas dataframe object
Entrez_ids.to_csv(sys.stdout, sep="\t") # write the dataframe to stdout
# sys.stdout.write() expects the character buffer object
Entrez_ids.to_csv("Entrez_ids.txt", sep="\t") # write the pandas dataframe to csv
My question is that if I use Entrez_ids.to_csv("Entrez_ids.txt", sep="\t"), the result file will not include the stderr like ' finished...), but Entrez_ids.to_csv(sys.stdout, sep="\t") will print out both the stderr message and also the dataframe.
How can I redirect the stderr if I use Entrez_ids.to_csv(sys.stdout, sep="\t") ? Thank you!
Upvotes: 0
Views: 243
Reputation: 9413
This can help you:
In python, can I redirect the output of print function to stderr?
import sys
sys.stderr = to_your_file_object
You can use devnull
to suppress the output:
import os
f = open(os.devnull,"w")
sys.stderr = f
Upvotes: 1