0

I'm trying to log the terminal display produced by a script which contains a Scipy optimization routine. I've tried three different ways, all with disappointing results.

open() and cmd-prompt redirection (">") did not log the warnings nor the per-iteration information I would like to keep track of; it only logged the parameter solutions.

logging.basicConfig() produced a blank file.

If possible, I would like to be able to log the exact terminal display of a script file. I've begun looking into using the logging module, but it seems pretty complex for what I would think is a very basic task (I'm coming from a Matlab background).

Environment:

. Python 2.7.3

. Operating Systems: Windows Vista and XP

LCJ
  • 22,196
  • 67
  • 260
  • 418
beets
  • 10,261
  • 4
  • 18
  • 11

2 Answers2

1

Probably the stuff you're interested in is being sent to stderr rather than stdout. Try redirecting both to a file. You can either do that in Python:

import sys

with open("log.txt", "w") as logfile:
    sys.stdout, sys.stderr = logfile, logfile

    # do your stuff here, e.g. import the module that you want to log

Or from the command line:

python myscript.py > log.txt 2>&1
kindall
  • 178,883
  • 35
  • 278
  • 309
  • 1
    The first method might not work if some scipy code prints from C(++) rather than going through Python printing channels (which is possible, idk). [This approach](http://stackoverflow.com/a/978264/344821) gets around that on Unix; not sure if it works on Windows. – Danica Aug 21 '12 at 03:46
  • Good point. You could of course use `subprocess` to run a separate Python instance and get around that, but at that point it's going to be easier to do it with the command line... – kindall Aug 22 '12 at 19:35
0

Take a look at linux script command. It saves everything that is printed in your terminal.

user@host:~ $ script
Script started, file is typescript
user@host:~ $ ls
bin  Documents  Pictures  Videos
user@host:~ $ exit # ^D stops script
Script done, file is typescript

Then, the result is in a file named typescript.

Valdir Stumm Junior
  • 4,568
  • 1
  • 23
  • 31