3

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:

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)

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

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

The traceback says it was expecting a string?

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

Thanks

MattDMo
  • 96,286
  • 20
  • 232
  • 224
SafetyNetter
  • 49
  • 1
  • 1
  • 4

3 Answers3

8

First, you want to make sure you open the file for writing:

f=open("output.txt","w")

Then you could use:

for r in result:
    print >> f, r

At the end of your script, make sure you use:

f.close()

Alternatively, you may need to cast your r to a string print >> f, str(r)

simhumileco
  • 27,137
  • 16
  • 123
  • 105
ode2k
  • 2,585
  • 12
  • 20
3

As others have mentioned, you need to specify that you want to write to the file, by using the argument 'w'.

It is best practice to use with when opening your file. It will notably ensure your file is closed properly, even if there's an exception.

with open('output.txt', 'w') as f:
    for r in result:
        f.write(str(r))
DevShark
  • 7,878
  • 7
  • 30
  • 54
0

It seems like result is a list, whereas write expects a string. So instead of f.write(result) you could try either f.write(str(result)) or, to get an output exactly as in your script:

for r in result:
    f.write(r)

Alternatively, you could import and then override the print function from Python 3, so that everything you print is automatically written to the file:

from __future__ import print_function

_print = print # reference to original print function
outfile = open("output.txt", "w") # file to write output to, mode is 'w' for writing
def print(*args, **kwargs):
    """new print function always writing to file"""
    _print(*args, file=outfile, **kwargs)

print("foo")
print("bar", end=" ")
print("blub")

outfile.close()

Or similarly, overwriting sys.stdout, and then just use print normally:

import sys
sys.stdout = open("output.txt", "w")
tobias_k
  • 78,071
  • 11
  • 109
  • 168