Will
Will

Reputation: 31

How to export python output from terminal (function code)?

I have a piece of code where I am trying to export the output. I tried this, but the only problem was where and what ~str~ had to be.

with open("piStorage.txt", "w") as fp:
    fp.write(~str~)

Here is my code to find ~str~

import csv
import time
start_time = time.time()

def calcPi(limit):
    q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
    decimal = limit
    counter = 0
    while counter != decimal + 1:
            if 4 * q + r - t < n * t:
                    yield n
                    if counter == 0:
                            yield '.'
                    if decimal == counter:
                            print('')
                            break
                    counter += 1
                    nr = 10 * (r - n * t)
                    n = ((10 * (3 * q + r)) // t) - 10 * n
                    q *= 10
                    r = nr
            else:
                    nr = (2 * q + r) * l
                    nn = (q * (7 * k) + 2 + (r * l)) // (t * l)
                    q *= k
                    t *= l
                    l += 2
                    k += 1
                    n = nn
                    r = nr
def main():
    pi_digits = calcPi(int(input(
        "Enter the number of decimals to calculate to: ")))
    i = 0
    for d in pi_digits:
            print(d, end='')
            i += 1
            if i == 200:
                print("")
                i = 0

if __name__ == '__main__':
    main()

print("--- %s seconds ---" % (time.time() - start_time))

with open("piStorage.txt", "w") as fp:
    fp.write(main)

I tried using this, but it doesn't work. It came up as: "

Traceback (most recent call last):
File "c:\Users\William\Desktop\pi.py", line 48, in <module>
fp.write(main)
TypeError: write() argument must be str, not function

"

with open("piStorage.txt", "w") as fp:
    fp.write(main)

Upvotes: 0

Views: 111

Answers (2)

John Gordon
John Gordon

Reputation: 33335

fp.write(main)

Since you're using a function name without parentheses (), this refers to the function object itself.

I think you meant to call the function by using parentheses:

fp.write(main())

This will call main() and write its returned value to the file.

However in this case that still won't work, because the way you've written main(), it doesn't actually return anything.

You need to change main() so that it writes to the file directly, instead of calling print().

Upvotes: 0

Unmitigated
Unmitigated

Reputation: 89294

You can use contextlib.redirect_stdout.

def main():
    pi_digits = calcPi(int(input(
        "Enter the number of decimals to calculate to: ")))
    i = 0
    from contextlib import redirect_stdout
    with open("piStorage.txt", "w") as fp:
        with redirect_stdout(fp):
            for d in pi_digits:
                    print(d, end='')
                    i += 1
                    if i == 200:
                        print("")
                        i = 0

Upvotes: 1

Related Questions