How do I redirect output to a file and to the console screen?

Discussion in 'Python' started by 28tommy, Dec 28, 2005.

  1. 28tommy

    28tommy Guest

    Hi there,

    I'm doing some TELNET and FTP sessions with my scripts. I need to
    redirect all the output (and not just what I print) to an output file,
    but still be able to see the session in process on the console.

    The Console screen is of less importance, so I could give it up, but
    what I'm looking for is a way to see all the interaction with the
    remote seesions on a file, that all the console data was redirected to
    it from within the script file.

    P.S. - the command "python script_file > output_file" is not from
    within the file, so it won't work for me.

    Thank you (-:
    28tommy, Dec 28, 2005
    #1
    1. Advertising

  2. The basic way to redirect output is to reassign a value to sys.stdout
    -- something along these lines:

    # redirect stdout to a disk file
    import sys
    saveout = sys.stdout
    outfile = open('output.txt', 'w')
    sys.stdout = outfile

    # output stuff
    print 'hello world'

    # restore stdout
    outfile.flush()
    outfile.close()
    sys.stdout = saveout:

    Essentially what you want is to have output redirected to *two*
    different streams at the same time, the original stdout and a file.
    Here's a link to a (very old) post on the subject that should help (see
    'class Tee') if coupled with the above:

    > http://groups.google.com/group/comp.lang.python/msg/5ab52448c1cbc10e


    -Martin
    Martin Miller, Dec 28, 2005
    #2
    1. Advertising

  3. 28tommy

    28tommy Guest

    Thank U.
    28tommy, Dec 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nishi Bhonsle

    Redirect output from console to a file

    Nishi Bhonsle, Jan 15, 2004, in forum: Java
    Replies:
    6
    Views:
    29,608
    Tony Morris
    Jan 18, 2004
  2. chuck amadi
    Replies:
    1
    Views:
    477
    Larry Bates
    Jun 23, 2004
  3. =?Utf-8?B?QUFPTSBUaW0=?=

    How can I redirect Console.Writeline to Response.Output?

    =?Utf-8?B?QUFPTSBUaW0=?=, Mar 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    637
    =?Utf-8?B?QUFPTSBUaW0=?=
    Mar 21, 2007
  4. Replies:
    5
    Views:
    3,411
    James Kanze
    Mar 4, 2008
  5. Mav
    Replies:
    22
    Views:
    354
Loading...

Share This Page