How can I fix this "ValueError: cannot have unbuffered text I / O" in python 3?

This is one of the questions of the MIT python project, but it is mostly written for python 2.x users, so is there a way to fix the following code to work in the latest python 3?

The current code raises the value "ValueError: cannot have unbuffered text I / O"

WORDLIST_FILENAME = "words.txt"

def load_words():

    print("Loading word list from file...")

    inFile = open(WORDLIST_FILENAME, 'r', 0)
    # wordlist: list of strings
    wordlist = []
    for line in inFile:
        wordlist.append(line.strip().lower())
    print("  ", len(wordlist), "words loaded.")
    return wordlist

      

+3


source to share


1 answer


From open

docstring:

... buffering is an optional integer used to set the buffering policy. Skip 0 to disable buffering ( only allowed in binary mode ) ...

So change inFile = open(WORDLIST_FILENAME, 'r', 0)



to

inFile = open(WORDLIST_FILENAME, 'r')

, or

inFile = open(WORDLIST_FILENAME, 'rb', 0)

if you really need it (which I doubt).

+3


source







All Articles