Q:

python output to text file

file = open(“testfile.txt”,”w”) 
 
file.write(“Hello World”) 
file.write(“This is our new text file”) 
file.write(“and this is another line.”) 
file.write(“Why? Because we can.”) 
 
file.close() 
36
f=open("Diabetes.txt",'r')
f.read()
12
file1 = open("MyFile.txt","w+") 

file1.readline()

fil1.write("string")

file1.close()
7
$ python my_program.py > output.txt
0
#Simple Mood Diary Script As An Example#

print('My Mood Diary')
mood = input('How are you feeling today? ')
#Saves the input as the variable 'mood'#
text_file = open("MoodDiary.txt", "w")
#Opens or creates the .txt file, sharing the directory of the script#
text_file.write(mood)
#Writes the variable into the .txt file#
text_file.close()
#Closes the .txt file#
-3

New to Communities?

Join the community