print "I can write my function to a txt file:"
my_function = raw_input("File name? ")
print_bills = input(bills(1010, 200, 100, 120))
open(my_func开发者_运维问答tion, 'w')
my_function.write('%r' % (print_bills))
my_function.close()
i'm working in python and am trying to get my function written to a txt file, the function goes like this
def bills(mortgage, elec, oil, ph_int_tv):
print "Our upcoming mortgage will be approximately %d /month)" % mortgage
print "Split between my brother and I, that will be %d /month" % (mortgage/2)
print "With a third roomate, it will be %d /month" % (mortgage/3)
print "My total bill, per month, with living costs included will be %d /month" % ((mortgage/2)+(elec/2)+(oil/2)+(ph_int_tv/2))
print "I better start saving money!!\n"
I'm pretty new, just started w/ the LPTHW online book... So my question is, what can i change in the top code to get this to work.... if anything?
You can reassign stdout
imporst sys
f = open("test.txt", "w")
sto = sys.stdout # save stdout so we can revert back to it
sys.stdout = f # from now on, anything that is printed will go to test.txt
# here, call any method that calls print
# or even
print "hello"
#finally when no more redirection is desired
f.close()
sys.stdout = sto # reestablish the regular stdout
You may also find useful hints for a slightly different purpose at this StackOverflow question. Beware that some of the solutions there may only apply to Unix systems.
Now, while the trick shown above is an expedient way of redirecting console (and/or stderr, BTW) output, and while the "what can i change in the top code to get this to work" in the question hinted at this kind of redirection approach. You may consider rewriting the bills() method by passing it a file, an invoking it with either sys.stdout or a file of your own choosing. Something like:
from __future__ import print_function # might as well get used to Python 3.0 print syntax
def bills(outFile, mortgage, elec, oil, ph_int_tv):
print("Our upcoming mortgage will be approximately %d /month)" % mortgage, file=outFile)
print("Split between my brother and I, that will be %d /month" % (mortgage/2), file=outFile)
print("With a third roomate, it will be %d /month" % (mortgage/3), file=outFile)
print("My total bill, per month, with living costs included will be %d /month"
% ((mortgage/2)+(elec/2)+(oil/2)+(ph_int_tv/2)), file=outFile)
print("I better start saving money!!\n", file=outFile)
def bills(mortgage, elec, oil, ph_int_tv):
print "Our upcoming mortgage will be approximately %d /month)" % mortgage
print "Split between my brother and I, that will be %d /month" % (mortgage/2)
print "With a third roomate, it will be %d /month" % (mortgage/3)
print "My total bill, per month, with living costs included will be %d /month" % ((mortgage/2)+(elec/2)+(oil/2)+(ph_int_tv/2))
print "I better start saving money!!\n"
def my_function(my_file,print_bills):
f=open(my_file, 'w')
f.write('%r' % (print_bills))
f.close()
print "I can write my function to a txt file:"
my_file = raw_input("File name? ")
print_bills = bills(1010, 200, 100, 120)
my_function(my_file, print_bills)
Basically, in Python, the standard output can be treated (almost) like any other file object. So you can assign a file object to sys.stdout
and print
statement will be written to the file instead of printed on the terminal window.
Something like the following might work.
import sys
myfile = open('myfile.txt', 'w')
stdout_bckp = sys.stdout
sys.stdout = myfile
bills(1010, 200, 100, 120)
sys.stdout = stdout_bckp
myfile.close()
If your on some sort of unix machine you can also redirect output from a program that you start from the terminal as such:
$ python myscript.py > myfile.txt
精彩评论