Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This is one of the MIT python project questions, but it's basically written for python 2.x users, so is there any way to fix the following code to operate in the latest python 3?

The current code is raising "ValueError: can't 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
like image 492
Richard Avatar asked Jul 23 '17 08:07

Richard


3 Answers

From open's docstring:

... buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode) ...

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

to

inFile = open(WORDLIST_FILENAME, 'r'), or to

inFile = open(WORDLIST_FILENAME, 'rb', 0) if you really need it (which I doubt).

like image 183
DeepSpace Avatar answered Oct 21 '22 08:10

DeepSpace


I could overcome this error by using code from this answer:

class Unbuffered(object):
    def __init__(self, stream):
        self.stream = stream

    def write(self, data):
        self.stream.write(data)
        self.stream.flush()

    def writelines(self, datas):
        self.stream.writelines(datas)
        self.stream.flush()

    def __getattr__(self, attr):
        return getattr(self.stream, attr)

import sys
sys.stdout = Unbuffered(sys.stdout)
like image 4
CharlesB Avatar answered Oct 21 '22 08:10

CharlesB


You can flush to the file after every write.

outFile = open(outputfile, "w",encoding='utf-8')

inFile = open(inputfile, "r",encoding='utf-8')

while inFile:
    line = inFile.readline()[:-1]
    outFile.write(line)
    outFile.flush()
like image 1
joaoreis Avatar answered Oct 21 '22 07:10

joaoreis