Pycharm expected type 'optional [bytes]' got 'str' instead

I use rsplitto separate the path name,

rootPath = os.path.abspath(__file__)
rootPath = (rootPath.rsplit('/', 1)[0]).rsplit('/', 1)[0]

But Pycharm warns

expected type optional [bytes]insteadstr

In python doche pointed out, using sepas a separator string.

So how to fix this?

+4
source share
1 answer

It seems to be rootPathseen as a byte object (maybe a small mistake?) Or a warning for the other part.

, , PyCharm , None, bytes. , Optional , Optional[type] - None type, bytes.

Python REPL , :

b'hello/world'.rsplit('/') # error bytes-like object required

byte:

b'hello/world'.rsplit(b'/') 

None, .

PyCharm rsplit .

+4

Source: https://habr.com/ru/post/1654327/


All Articles