0

I have read Why is Python giving me "an integer is required" when it shouldn't be?. It is nearly a complete solution for my issue, but not quite.

Here is my code:

#! usr/local/bin/python3.6
# coding: utf-8

from os import getcwd, listdir
import psycopg2
from io import open

conn = psycopg2.connect("dbname=ktab user=malikarumi")
cur = conn.cursor()
path = getcwd()
filenames = listdir(path)

for filename in filenames:
with open(filename, 'r', 'utf-16') as f:
    f1 = f.read()

    cur.execute("INSERT INTO testable (title, content, chron_date, clock)\
    VALUES (%s, %s, %s, %s)"),
    (filename, f1, '2017-12-30', '23:59:00'),

conn.commit()
cur.close()
conn.close()

After reading the older SO post, I modified my code as above, so as to specify which io module I wanted and restrict the os modules to the two I was using. Sublime Text 3 has a tooltip which indicates I am calling io.open(), but when I run the code, it is plainly os.open() (I know because of the error):

(lifeandtimes) malikarumi@Tetuoan2:~/Projects/Progress_Logs/2017$
python ktab_odt4.py
Traceback (most recent call last):
File "ktab_odt4.py", line 17, in <module>
with open(filename, 'r', 'utf-16') as f:
TypeError: an integer is required (got type str)

Note: I already tried with io.open() and import io. In both cases pylint squawked and the interpreter gave me name errors. Using 'rb' got past pylint but the interpreter still gives me the TypeError. What is the workaround for this situation?

3
  • Please reduce the code in your question to the bare minimum needed to reproduce the problem. Commented Jan 1, 2018 at 19:44
  • @martineau: Seriously? Is that a requirement now? I ask because I have more often been taken to task for not putting enough code in my posts! Commented Jan 2, 2018 at 2:22
  • Yes, you should almost always include code, but try to provide the least amount of it that will reproduce the problem. This will allow others to reproduce the problem and test any fixes they have and it will make it more likely for you to get help quickly. See How to create a Minimal, Complete, and Verifiable Example. In this case all the psycopg2/database related stuff just gets in the way. Commented Jan 2, 2018 at 4:59

1 Answer 1

1

The third positional parameter to io.open() is buffering, which requires an integer. You are passing the string 'utf-16' as the third parameter, thus the error you're getting. You need to use encoding='utf-16' to pass that value as a keyword parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

It looks like you're right. I tried it and this error goes away. Thank you. Sadly, now I have "UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 260-261: illegal encoding" which seems to be in the document itself, and a different issue I will have to post about separately if I can't figure out a workaround :-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.