By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,369 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,369 IT Pros & Developers. It's quick & easy.

Help with python output redirection

P: n/a
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.

Apr 15 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Fri, 14 Apr 2006 16:59:13 -0700, fatalserpent wrote:
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.


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.

Apr 15 '06 #2

P: n/a
Thank you. Also this script is PublicDomain right?
Steven D'Aprano wrote:
On Fri, 14 Apr 2006 16:59:13 -0700, fatalserpent wrote:
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.


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.


Apr 15 '06 #3

P: n/a
On 16/04/2006 12:36 AM, fatal.serpent top-posted:
Thank you. Also this script is PublicDomain right?
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.
Steven D'Aprano wrote:
On Fri, 14 Apr 2006 16:59:13 -0700, fatalserpent wrote:
[snip] 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.

Apr 15 '06 #4

P: n/a
On Sat, 15 Apr 2006 07:36:30 -0700, fatal.serpent wrote:
Thank you. Also this script is PublicDomain right?
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.
Steven D'Aprano wrote:
On Fri, 14 Apr 2006 16:59:13 -0700, fatalserpent wrote:
> 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.


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.


--
Steven.

Apr 16 '06 #5

P: n/a
Steven D'Aprano wrote:
So, let me say firstly that I wish the following code to be put into
the public domain


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>

Apr 16 '06 #6

P: n/a
Fredrik Lundh wrote:
Steven D'Aprano wrote:
So, let me say firstly that I wish the following code to be put into
the public domain


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 ?


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

--
Robert Kern
ro*********@gmail.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

Apr 16 '06 #7

P: n/a
On Sun, 16 Apr 2006 12:16:39 +0200, Fredrik Lundh wrote:
Steven D'Aprano wrote:
So, let me say firstly that I wish the following code to be put into
the public domain


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 ?


???

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 misunderstanding:

- 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.

Apr 16 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.