How to use xbmcvfs.File() and io.open()?
#1
Hi,

I want my addon to be able to read text files on nfs:// and smb:// shares.

For this I need xbmcvfs.File().

So what is the xbmcvfs.File() equivalent of:

python:
import io
 
with io.open('foo.txt', 'r', encoding='utf-8') as fo:
text = fo.read()

Thanks!

PS: I read the sticky, but I don't even know where to start to combine these 2 'open' functions...
Add-on : Bluray iso utils Rewrite of BR_iso_enhancements
Add-on : BR_iso_Enhancements Give theatrical & directors cut from the same bluray iso each their own library entry, use bluray iso's of tv shows as if they are already ripped.
Reply
#2
Syntax is the same for Python 2 and 3:
python:
from contextlib import closing
from xbmcvfs import File

with closing(File('/path/to/file')) as fo:
    text = fo.read()

However, the big catch is that the current Python 2 implementation returns a binary string, but in Python 3 .read() methods decodes text using UTF-8 encoding (that is the default for Python 3). For binary data you need to use .readBytes().

I can also suggest this helper library https://github.com/romanvm/kodi.six that normalizes string handling in Python 2 and 3.
Reply

Logout Mark Read Team Forum Stats Members Help
How to use xbmcvfs.File() and io.open()?0