I have done a program with 4 functions, now I have to add the results produced by the functions into a text file from python. How can I do that?
Asked
Active
Viewed 211 times
-2
-
4Possible duplicate of [Correct way to write line to file?](https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file) – Sayse Nov 11 '19 at 15:21
-
I want to know how to add a function in a text file not a string – Jayram Singh Nov 11 '19 at 15:52
-
There is no difference, what have you tried/researched? – Sayse Nov 11 '19 at 15:53
-
An error is shown ... function object not iterable – Jayram Singh Nov 11 '19 at 15:55
-
Please create a [mcve] and read [ask], although your question is still a duplicate. – Sayse Nov 11 '19 at 15:56
-
Using a function in place of String....error comes saying function object not iterable – Jayram Singh Nov 12 '19 at 06:19
-
You need to make a [mcve] – Sayse Nov 12 '19 at 08:49
1 Answers
0
open a text file where you want to save and put it in the write mode
def func1(a, b):
return a+b
def func2(a, b):
return a-b
def func3(a, b):
return a*b
def func4(a, b):
return a/b
txt_write = open('file_name', 'w+')
txt_write.writelines(func1(2,6))
txt_write.writelines(func2(7,1))
txt_write.writelines(func3(6,3))
txt_write.writelines(func4(9,3))
txt_write.close()
use writelines
to write in the text file. Don't forget to close the file.

Fahim Ahmed
- 141
- 11