redirecting print to a a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AndyL

    #1

    redirecting print to a a file

    Hi,

    Can I redirect print output, so it is send to a file, not stdout.

    I have a large program and would like to avoid touching hundreds of print's.

    Thx, Andy
  • Harold Fellermann

    #2
    Re: redirecting print to a a file

    >>> import sys[color=blue][color=green][color=darkred]
    >>> sys.stdout = file("output"," w")
    >>> print "here you go"[/color][/color][/color]

    Comment

    • AndyL

      #3
      Re: redirecting print to a a file

      Harold Fellermann wrote:[color=blue][color=green][color=darkred]
      >>>>import sys
      >>>>sys.stdou t = file("output"," w")
      >>>>print "here you go"[/color][/color]
      >
      >[/color]


      And what if I want to still send the output to stdout and just a log it
      in the file as well?

      A.

      Comment

      • Sybren Stuvel

        #4
        Re: redirecting print to a a file

        AndyL enlightened us with:[color=blue]
        > Can I redirect print output, so it is send to a file, not stdout.[/color]

        Change sys.stdout to a file object.
        [color=blue]
        > I have a large program and would like to avoid touching hundreds of
        > print's.[/color]

        I can suggest using the logging module instead of print. It's much
        more flexible than prints, and well suited for large programs.

        Sybren
        --
        The problem with the world is stupidity. Not saying there should be a
        capital punishment for stupidity, but why don't we just take the
        safety labels off of everything and let the problem solve itself?
        Frank Zappa

        Comment

        • AndyL

          #5
          Re: redirecting print to a a file

          Sybren Stuvel wrote:[color=blue][color=green]
          >>I have a large program and would like to avoid touching hundreds of
          >>print's.[/color]
          >
          >
          > I can suggest using the logging module instead of print. It's much
          > more flexible than prints, and well suited for large programs.
          >[/color]

          Thx for the hint. I will look into that.

          A.

          Comment

          • Sybren Stuvel

            #6
            Re: redirecting print to a a file

            AndyL enlightened us with:[color=blue]
            > And what if I want to still send the output to stdout and just a log
            > it in the file as well?[/color]

            $ python some_program.py | tee output.log

            Or write a class that has a write() function and outputs to a file and
            to the original value of sys.stdout (IIRC that's in sys.__stdout__)

            Sybren
            --
            The problem with the world is stupidity. Not saying there should be a
            capital punishment for stupidity, but why don't we just take the
            safety labels off of everything and let the problem solve itself?
            Frank Zappa

            Comment

            • AndyL

              #7
              Re: redirecting print to a a file

              Sybren Stuvel wrote:[color=blue]
              > AndyL enlightened us with:
              >[color=green]
              >>And what if I want to still send the output to stdout and just a log
              >>it in the file as well?[/color]
              >
              >
              > $ python some_program.py | tee output.log
              >
              > Or write a class that has a write() function and outputs to a file and
              > to the original value of sys.stdout (IIRC that's in sys.__stdout__)
              >
              > Sybren[/color]

              Thx again. Python is cool, do that in C++ or Java :-)

              A.

              Comment

              • Mirco Wahab

                #8
                Re: redirecting print to a a file

                AndyL wrote:[color=blue][color=green]
                >> $ python some_program.py | tee output.log[/color]
                > Thx again. Python is cool, do that in C++ or Java :-)[/color]

                Yes, thats obviously true ;-)

                $> javac someprogram.jav a;
                $> java someprogram | tee output.log

                $> g++ someother.cpp -o someother
                $> ./someother | tee output.log

                SCNR,

                Mirco

                Comment

                Working...