saving output as txt

How do I use python to auto generate and save a .txt file?

Example:
a = 10
b = 200
r = a*b
if r>1000:
s = 1
else:
s = 2
output = [a,b,r,s]

I want to automatically save the output as a .txt file, so I can use it in other programs.

First you need to define a file to be made/opened
From that you can print to that specific file or append to it or read it as your case dictates
it would look like this in code:
“”"
myfile = file(‘output.txt’,‘w’)
print >> myfile, output
myfile.close()
“”"
You can simply add to this by saying:
“”"
myfile = file(‘output.txt’,‘a’)
print >> myfile, variable,
myfile.close()
“”"
If you want the file to be in a specific location on your machine, just put the path in the quotes where you define the name
Also, if you are wanting to auto open this file after you run your script for whatever reason you can do this:
“”"
import os
os.startfile(‘C:/location of file/file name’) - windows
os.system(‘C:/location of file/file name’) = linux
“”"
Hope this helps and good luck!!