3
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_function, '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?

New_Guy101
  • 31
  • 1

3 Answers3

3

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)
Community
  • 1
  • 1
mjv
  • 73,152
  • 14
  • 113
  • 156
  • I think I see where your going here, but as I said, i'm new and still learning. So if I import sys, set the txt file to f, and set all standard outputs to f, then running my function will print it to the txt file? – New_Guy101 Jul 12 '11 at 21:06
  • @New_Guy101. Exactly! Once sys.stdout is reassigned, anything that would normally go to the screen goes to the text file. Note how once we're done, we reassign stdout back to its original value. Also note Wim's response below where the `with construct` is used along with exception catching and a finaly clause to ensure that no matter what, stdout gets properly reset to its orig. value. Also note my remark that while insightful and convenient this approach (of reassigning stdout) is probably not the recommended way of introducing logging/piping. – mjv Jul 13 '11 at 01:56
  • absolutely, i'm still trying to fully grasp 'import sys', though I think i'm starting to get it... I understand the idea of setting standard outputs to a txt file, but I don't quite get changing the standard output and changing it back. I've read a lot of the pydoc stuff and understand some things but I'm really trying to find something that puts a lot of this stuff into layman terms, so I can start fully grasping what programmers are trying to say.... also, this worked prefectly so thanks for that too :-) – New_Guy101 Jul 13 '11 at 05:51
0

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
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Loïc Séguin-C.
  • 1,806
  • 2
  • 13
  • 14
0
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)
Mike
  • 1
  • thanks bro... i tried this but unfortunately it didn't work. The error msg gave me a python link to something about a Back_to_the_future function... I read it but didn't quite understand what they were talking about. Next time we chill you gotta give me the full breakdown – New_Guy101 Jul 13 '11 at 05:56