I want to check that I’m doing something correctly in the wake of switching from Python 2 to Python 3, if I may.
I have a bit of code that reads a text-file–in this case my “license/credits” file–and then loads it into a TextNode. This is done by simply calling “VirtualFileSystem.readFile”–which returns a string–and then passing the result into “TextNode.setText”.
In Python 2, this worked as expected. In Python 3, it’s a little more complicated: it seems that “readFile” returns a byte-string, and “setText” doesn’t accept those.
My solution right now is to call ‘decode(“utf-8”)’ on the byte-string, returning a Unicode string (if I have it correct). This seems to work.
For reference, here is my current solution in code-form:
vfs = VirtualFileSystem.getGlobalPtr() creditString = vfs.readFile(Filename("license.txt"), False) creditString = creditString.decode("utf-8") creditText.setText(creditString)
However, I’m not confident that I’m doing quite the right thing, or that I have the right codec, or that there won’t be some variance on other operating systems (or even just other machines).
So, am I doing the above correctly? Are there any caveats that I should be aware of here?