Skip to content Skip to sidebar Skip to footer

Typeerror When Trying To Convert Python 2.7 Code To Python 3.4 Code

I am having issues converting the code below which was written for Python 2.7 to code compatible in Python 3.4. I get the error TypeError: can't concat bytes to str in the line out

Solution 1:

The problem is that GzipFile needs to wrap a bytes-oriented file object, but you're passing a StringIO, which is text-oriented. Use io.BytesIO instead:

from io import BytesIO  # Works even in 2.x

# snip

response = urllib.request.urlopen(baseURL + filename)
compressedFile = BytesIO()  # change this
compressedFile.write(response.read())  # and this

compressedFile.seek(0)

decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode='rb') 

with open(outFilePath, 'w') as outfile:
    outfile.write(decompressedFile.read().decode("utf-8", errors="ignore"))
    # change this too

Post a Comment for "Typeerror When Trying To Convert Python 2.7 Code To Python 3.4 Code"