Schiffer
Schiffer

Reputation: 21

psycopg2 connec.close() causes syntax error

I have this function:

conn = psycopg2.connect("dbname=test user=t password=test host=localhost")
cur = conn.cursor()
n=re.compile(r"(Number of atoms\s*:?\s*([0-9]+))")
f = open(filename,'rb')
new=open("coord.txt",'w')
while True:
    line = f.readline()
    nA=n.search(line)
    if nA != None:
       new.writelines(nA.group(0)+"\n")
binaryC=new.read()
cur.execute("INSERT INTO fhi(archivo) VALUES (%s)",(psycopg2.Binary(binaryF),))
conn.commit()
new.close()
cur.close()
conn.close()

when I run it I get this error

conn.close()
   ^
SyntaxError: invalid syntax

I don't see the error, help me please

Upvotes: 0

Views: 233

Answers (1)

Josh Wright
Josh Wright

Reputation: 2505

That code is really a mess...

I assume there is more context above this, and that's where the issue is (probably missing a closing paren). There is no syntax issue in this particular snippet, but there are tons of style issues (which can hide syntax issues).

Please consider running some sort of linter (pyflakes, pep8, etc) to make sure your code's style is consistent. It will make your life much easier in general.

Upvotes: 1

Related Questions