Python 2.7.11 write output to a text file
<p>I am new to python and I am trying to figure out how to write the results to a text file. Here is my code:</p>

<pre><code>from os import getenv
import sqlite3

conn = sqlite3.connect(getenv("APPDATA") + "\..\Local\...sanitized")
conn3 = sqlite3.connect(getenv("APPDATA") + "\..\Local\...sanitized")
conn1 = sqlite3.connect(getenv("APPDATA") + "\..\Local\...sanitized")
cursor3 = conn3.cursor()
cursor1 = conn1.cursor()
cursor = conn.cursor()
cursor.execute('SELECT ...sanitized')
for result in cursor.fetchall():
    ...sanitized
    if password:
        print "Site: " + result[0]
        print 'Username: ' + result[1]
        print 'Password: ' + password
        print '---------------------------------------------'
cursor3.execute("...sanitized") 
print("fetchall:")
result = cursor3.fetchall() 
for r in result:
    print(r)

cursor1.execute("...sanitized") 
print("fetchall:")
result = cursor1.fetchall() 
for r in result:
    print(r)
</code></pre>

<hr>

<p>I have tried f = open('output.txt') at the beginning of the code and placed </p>

<pre><code>f.write(r) # and I tried 
f.write(result) # after defining it near bottom as these are my variables 
f.close()# :(
</code></pre>

<p>The traceback says it was expecting a string?</p>

<p>Can someone help explain what should go into the print(here) ? I am trying to grasp the concepts but don't quite get it yet</p>

<p>Thanks</p>