Help with python output redirection

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

    #1

    Help with python output redirection

    Here is the basic code (yes, I know its tiny).

    x = ['print "x =", x', 'for m in x: print m']
    print "x =", x
    for m in x: print m

    I want to modify this so it will output to a file called 1. What I want
    is to have that file direct its output to a file called 2 and 2 direct
    to 3 and so on. Hopefully this will be an easy-to-answer question. THX
    in advance.

  • Steven D'Aprano

    #2
    Re: Help with python output redirection

    On Fri, 14 Apr 2006 16:59:13 -0700, fatalserpent wrote:
    [color=blue]
    > Here is the basic code (yes, I know its tiny).
    >
    > x = ['print "x =", x', 'for m in x: print m']
    > print "x =", x
    > for m in x: print m
    >
    > I want to modify this so it will output to a file called 1. What I want
    > is to have that file direct its output to a file called 2 and 2 direct
    > to 3 and so on. Hopefully this will be an easy-to-answer question. THX
    > in advance.[/color]

    From the shell, you are probably doing something like this:

    $ python mymodule.py

    Change it to this:

    $ python mymodule.py > 2
    $ python 2 > 3
    $ python 3 > 4

    and so on.

    Alternatively, you can do this:

    x = ['f = file("2", "w")', 'print >>f, "x =", x',
    'for m in x: >>f, print m']
    print >>f, "x =", x
    for m in x: print >>f, m

    I'll leave changing the file name from "2" to "3" etc. as an exercise for
    you.


    --
    Steven.

    Comment

    • fatal.serpent

      #3
      Re: Help with python output redirection

      Thank you. Also this script is PublicDomain right?
      Steven D'Aprano wrote:[color=blue]
      > On Fri, 14 Apr 2006 16:59:13 -0700, fatalserpent wrote:
      >[color=green]
      > > Here is the basic code (yes, I know its tiny).
      > >
      > > x = ['print "x =", x', 'for m in x: print m']
      > > print "x =", x
      > > for m in x: print m
      > >
      > > I want to modify this so it will output to a file called 1. What I want
      > > is to have that file direct its output to a file called 2 and 2 direct
      > > to 3 and so on. Hopefully this will be an easy-to-answer question. THX
      > > in advance.[/color]
      >
      > From the shell, you are probably doing something like this:
      >
      > $ python mymodule.py
      >
      > Change it to this:
      >
      > $ python mymodule.py > 2
      > $ python 2 > 3
      > $ python 3 > 4
      >
      > and so on.
      >
      > Alternatively, you can do this:
      >
      > x = ['f = file("2", "w")', 'print >>f, "x =", x',
      > 'for m in x: >>f, print m']
      > print >>f, "x =", x
      > for m in x: print >>f, m
      >
      > I'll leave changing the file name from "2" to "3" etc. as an exercise for
      > you.
      >
      >
      > --
      > Steven.[/color]

      Comment

      • John Machin

        #4
        Re: Help with python output redirection

        On 16/04/2006 12:36 AM, fatal.serpent top-posted:[color=blue]
        > Thank you. Also this script is PublicDomain right?[/color]

        Wrong. If you attempt to turn "The D'Aprano Code" into a best-seller,
        and don't donate assign all of your royalties to the PSF, they will
        pursue you and your descendants to the ends of the universe.
        [color=blue]
        > Steven D'Aprano wrote:[color=green]
        >> On Fri, 14 Apr 2006 16:59:13 -0700, fatalserpent wrote:
        >>[/color][/color]
        [snip][color=blue][color=green]
        >> From the shell, you are probably doing something like this:
        >>
        >> $ python mymodule.py
        >>
        >> Change it to this:
        >>
        >> $ python mymodule.py > 2
        >> $ python 2 > 3
        >> $ python 3 > 4
        >>
        >> and so on.
        >>
        >> Alternatively, you can do this:
        >>
        >> x = ['f = file("2", "w")', 'print >>f, "x =", x',
        >> 'for m in x: >>f, print m']
        >> print >>f, "x =", x
        >> for m in x: print >>f, m
        >>
        >> I'll leave changing the file name from "2" to "3" etc. as an exercise for
        >> you.
        >>
        >>
        >> --
        >> Steven.[/color]
        >[/color]

        Comment

        • Steven D'Aprano

          #5
          Re: Help with python output redirection

          On Sat, 15 Apr 2006 07:36:30 -0700, fatal.serpent wrote:
          [color=blue]
          > Thank you. Also this script is PublicDomain right?[/color]

          Oh heavens, I never even thought about that... the world was a much better
          place when you actually needed to *claim copyright* rather than just have
          it apply automatically on every stupid little doodle or comment.

          I've heard from authorities I trust that under US law private citizens
          can't "not copyright" something you write, even if you want it to go into
          the Public Domain. I've also heard the opposite from experts I equally
          trust.

          So, let me say firstly that I wish the following code to be put into
          the public domain, and if there is any reason why it has not been, I
          hereby give everybody an unlimited, non-exclusive, transferable, free of
          all charges and royalties, licence to use the following code in any way
          they see fit, with no conditions attached except there is no warranty. You
          don't even have to credit me. If you try to make a warranty claim against
          me for this code, the licence is instantly revoked.

          There. It probably won't stand up in a court of law, but I promise not to
          sue if you promise the same.
          [color=blue]
          > Steven D'Aprano wrote:[color=green]
          >> On Fri, 14 Apr 2006 16:59:13 -0700, fatalserpent wrote:
          >>[color=darkred]
          >> > Here is the basic code (yes, I know its tiny).
          >> >
          >> > x = ['print "x =", x', 'for m in x: print m']
          >> > print "x =", x
          >> > for m in x: print m
          >> >
          >> > I want to modify this so it will output to a file called 1. What I want
          >> > is to have that file direct its output to a file called 2 and 2 direct
          >> > to 3 and so on. Hopefully this will be an easy-to-answer question. THX
          >> > in advance.[/color]
          >>
          >> From the shell, you are probably doing something like this:
          >>
          >> $ python mymodule.py
          >>
          >> Change it to this:
          >>
          >> $ python mymodule.py > 2
          >> $ python 2 > 3
          >> $ python 3 > 4
          >>
          >> and so on.
          >>
          >> Alternatively, you can do this:
          >>
          >> x = ['f = file("2", "w")', 'print >>f, "x =", x',
          >> 'for m in x: >>f, print m']
          >> print >>f, "x =", x
          >> for m in x: print >>f, m
          >>
          >> I'll leave changing the file name from "2" to "3" etc. as an exercise for
          >> you.
          >>
          >>
          >> --
          >> Steven.[/color][/color]



          --
          Steven.

          Comment

          • Fredrik Lundh

            #6
            Re: Help with python output redirection

            Steven D'Aprano wrote:
            [color=blue]
            > So, let me say firstly that I wish the following code to be put into
            > the public domain[/color]

            so you're taking the work by others ("for m in x", "print m", and so on)
            and think that by posting it to usenet as if you wrote it yourself, you
            can release it into the public domain ?

            </F>



            Comment

            • Robert Kern

              #7
              Re: Help with python output redirection

              Fredrik Lundh wrote:[color=blue]
              > Steven D'Aprano wrote:
              >[color=green]
              >>So, let me say firstly that I wish the following code to be put into
              >>the public domain[/color]
              >
              > so you're taking the work by others ("for m in x", "print m", and so on)
              > and think that by posting it to usenet as if you wrote it yourself, you
              > can release it into the public domain ?[/color]

              I don't think there's a judge in the world that would think those few characters
              are copyrightable by themselves.

              --
              Robert Kern
              robert.kern@gma il.com

              "I have come to believe that the whole world is an enigma, a harmless enigma
              that is made terrible by our own mad attempt to interpret it as though it had
              an underlying truth."
              -- Umberto Eco

              Comment

              • Steven D'Aprano

                #8
                Re: Help with python output redirection

                On Sun, 16 Apr 2006 12:16:39 +0200, Fredrik Lundh wrote:
                [color=blue]
                > Steven D'Aprano wrote:
                >[color=green]
                >> So, let me say firstly that I wish the following code to be put into
                >> the public domain[/color]
                >
                > so you're taking the work by others ("for m in x", "print m", and so on)
                > and think that by posting it to usenet as if you wrote it yourself, you
                > can release it into the public domain ?[/color]

                ???

                You're joking, right?

                Look, I'm not having a good run at the moment telling when folks are being
                funny, so if you trying to introduce a note of levity into the discussion,
                ha ha I laughed so much my sides burst.

                But if you are being serious, and for the benefit of anyone else out there
                who doesn't get the joke and thinks there is something of substance to
                your claim:

                (1) If I didn't already have the copyright to the code, I simply can't
                transfer ownership to it or reassign copyright no matter what I say;

                (2) If you read my post, I think it is abundantly clear that my intention
                was not to claim copyright on it at all;

                (3) Only because the Original Poster appeared to be concerned about
                copyright on the code I posted, I released it, simply to reassure the OP;

                (4) As far as me posting individual Python statements (like for, print
                etc.) as if I wrote them myself, that's ludicrous. A writer's copyright on
                a work does not imply copyright on the individual words; likewise a
                programmer's copyright on a program does not mean he claims copyright on
                the language itself.

                So, just so there is no misunderstandin g:

                - I did not write the Python language, I do not have ownership of the
                copyright to the Python language, and it is not my intention to transfer
                copyright of the Python language (in whole or in part) to the public
                domain, even if I were able to, which I am not.

                - I do not have copyright on any code not written by me, consequently I'm
                not releasing the copyright on any code not written by me.

                - I am, however, explicitly releasing from copyright the code written by
                me in the earlier message with the subject line "Re: Help with python
                output redirection" dated Sat, 15 Apr 2006 12:09:17 +1000, and then
                re-posted as quoted text in a message with the same subject line dated
                Sun, 16 Apr 2006 11:35:04 +1000.

                - In the event that the appropriate laws do not allow me to release from
                copyright the code, I offer it to anyone who wants to copy such a trivial
                few lines of code into their own work with an unlimited, non-exclusive,
                transferable, warranty-less licence.

                - None of this is a substitute for paying a lawyer many hundreds of
                dollars to be told that no judge is going to rule copyright infringement
                for copying such a trivial work, and that this whole exercise was an utter
                waste of time.


                Sheesh. I have to deal with enough legal waffle at work, I didn't think
                I'd be held to such pedantic standards about a trivial four-line piece of
                code on Usenet.

                Oh, Fredrik, if your intention was to demonstrate just what a PITA overly
                strict copyright law is, you're preaching to the converted brother.


                --
                Steven.

                Comment

                Working...