gabriel belini gabriel belini - 1 year ago 90
Linux Question

How to redirect the output of my program into a .txt file using Linux

I need to redirect the output of a function into a .txt file.
I'm using the function printClassTree() from the library ontospy.
The code of my program is very simple:

import ontospy
g = ontospy.Graph("/home/gabsbelini/Documentos/ontologiaTeste.owl")

I've tried:

python > file.txt

But it does not work.
I'm using Ubuntu 14.04
When I execute the command above it just creates the "file.txt" and shows the output in the terminal (I want that to be saved in file.txt)

Answer Source

try tee:

python | tee file.txt

or from python

with open('file.txt', 'w') as fh:

EDIT In case is going to stderr you can try:

python 2>&1 | tee file.txt


 with open('file.txt', 'w') as fh:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download