[Tutor] TypeError when io.open is used

petkovas at dir.bg petkovas at dir.bg
Wed Jun 23 14:03:06 CEST 2010


Hello all!

I would like to ask you the following: what should i do to be executed 
my correctly?

The error:
Traceback <most recent call last>:
   File "insert_into_db_v9.py", line 55, in <module>
TypeError: an integer is required

The code i use is the following:

import psycopg2
import os
from os import sep, listdir, path
import io

libfolder = "C:\\Blender_Library\\BlenderLib\\objectLib\\"

#we are in C:\\Blender_Library\\BlenderLib\\objectLib\\
for dir1 in os.listdir(libfolder):
	print libfolder + dir1
	if os.path.isdir(os.path.dirname(libfolder + dir1)):	
		tempdir = libfolder + dir1
		tempfiles = os.listdir(tempdir)
		#we are for example in 
C:\\Blender_Library\\BlenderLib\\objectLib\\Osaka"
		for f in tempfiles:
			if os.path.isdir(libfolder + dir1 + os.sep + f):
				tempdir1 = libfolder + dir1 + os.sep + f
				tempfiles1 = os.listdir(tempdir1)
				#we are for example in 
C:\\Blender_Library\\BlenderLib\\objectLib\\Faqns\\Osaka"
				for ff in tempfiles1:
					print libfolder + dir1 + os.sep + f + os.sep + ff
					if ff[-5:] == "blend":
						# !!! that is test data. It must be changed
						conn=psycopg2.connect("host=localhost dbname=postgres 
user=postgres password=test")

						#conn.cursor will return a cursor oject, you can use this cursor 
to perform queries
						cursor = conn.cursor()

						# psycopg2.Binary() escapes all data that needs that
						data1 = psycopg2.Binary(io.open( tempdir1 + os.sep + ff, 'rb' 
).read())
	
						# execute our Query
						cursor.execute("""UPDATE testtable SET blend = %s)
							WHERE testtable_n = %s""", data1, 
str(os.path.splitext(file)[0]))

						# Save (commit) the changes
						conn.commit()
		
						# We can also close the cursor if we are done with it
						cursor.close()
				elif ff[-3:] == "jpg" or ff[-4:] == "jpeg":
						# !!! that is test data. It must be changed
						conn=psycopg2.connect("host=localhost dbname=postgres 
user=postgres password=test")

						#conn.cursor will return a cursor oject, you can use this cursor 
to perform queries
						cursor = conn.cursor()

						# psycopg2.Binary() escapes all data that needs that
						data1 = psycopg2.Binary(io.open( tempdir1 + os.sep + ff, 'rb' 
).read())
	
						# execute our Query
						cursor.execute("""UPDATE testtable SET jpeg = %s)
							WHERE testtable_n = %s""", data1, 
str(os.path.splitext(file)[0]))

						# Save (commit) the changes
						conn.commit()
		
						# We can also close the cursor if we are done with it
						cursor.close()
					elif ff[-3:] == "txt":
						# !!! that is test data. It must be changed
						conn=psycopg2.connect("host=localhost dbname=postgres 
user=postgres password=test")

						#conn.cursor will return a cursor oject, you can use this cursor 
to perform queries
						cursor = conn.cursor()


More information about the Tutor mailing list