0

Using a save_ouput function, how can I save the output of every function?

def a():
    print("abc,")

def b():
    print("help,")

def c():
    print("please")

def save_output():
    # save output of all functions

def main():
    a()
    b()
    c()
    save_output()

main()

^So it'd save abc,help,pleaseas a text file when main is called

zamir
  • 2,144
  • 1
  • 11
  • 23
  • 1
    Your `save_output()` function doesn't do anything. You need to explicity tell it what to do you want to save and where do you want to save. There should be code instead of just a comment `#save output of all functions`. – joaoavf Feb 22 '18 at 22:54
  • Several ways, did you search for one and try it? – roganjosh Feb 22 '18 at 22:55
  • @joaoavf That's a placeholder for "insert someone else's code here". It's like the opposite of a parameterized SQL statement – roganjosh Feb 22 '18 at 22:56
  • I know it needs code, I just dont know what. I've tried: – Gskgskgx Bczkgxlhx Feb 22 '18 at 22:56
  • with io.open("abc.txt.", 'w') as f: with redirect_stdout(f): #What print statements here? – Gskgskgx Bczkgxlhx Feb 22 '18 at 22:57
  • Not exactly a duplicate, but this question discusses a lot of these issues: [redirect stdout](https://stackoverflow.com/questions/616645/how-do-i-duplicate-sys-stdout-to-a-log-file-in-python) – John Anderson Feb 22 '18 at 23:02

2 Answers2

0

You can't with your current structure, at least not without something crazy like inspecting the terminal (which might well not exist).

The right way to do this is by redirecting standard out before you call the other functions:

import sys
def a():
  print("abc,")
def b():
  print("help,")
def c():
  print("please")

def main():
 original_stdout = sys.stdout
 sys.stdout = open('file', 'w')
 a()
 b() 
 c()
 sys.stdout = original_stdout

main()
# now "file" contains "abc,help,please"

However, it's also worth asking why you want to do this at all – there are many more straightforward ways to write to a file that don't involve messing with stdout, which might have unintended consequences. Can you describe your use case a little more fully?

Personman
  • 2,324
  • 1
  • 16
  • 27
  • Thanks for your comment. I have several functions performing things (like appending) to some text, which is then added to by each function. I want to print the output of each function with a save_output function – Gskgskgx Bczkgxlhx Feb 22 '18 at 23:01
  • [This answer](https://stackoverflow.com/a/616686/7207392) may be relevant. It's a recipe how to duplicate `stdout` so the "normal" output is not lost. – Paul Panzer Feb 22 '18 at 23:13
  • @GskgskgxBczkgxlhx is there some way in which my method isn't meeting your goal? I updated the answer to more explicitly mirror your code in case anything was confusing. – Personman Feb 22 '18 at 23:15
0

Would you consider something like this

def a():
    return 'abc'
def b():
    return 'help'
def c():
    return 'please'
def save_output(file_name, *args):
    with open(file_name, 'w') as f:
        for x in args:
            f.write('Function {0}() returned: {1}{2}'.format(x.__name__,x(),'\n'))

To test:

save_output('example.txt',a,b,c)

Output:

Function a() returned: abc
Function b() returned: help
Function c() returned: please
Anthony
  • 421
  • 1
  • 5
  • 13