Help with python output redirection

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Apr 14 22:09:17 EDT 2006


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.




More information about the Python-list mailing list