Expected type 'optional[bytes]' got 'str' instead

Peter Otten __peter__ at web.de
Tue Sep 13 07:37:35 EDT 2016


Daiyue Weng wrote:

> Hi, I have the following piece of code,
> 
> rootPath = os.path.abspath(__file__)
> 
> rootPath = (rootPath.rsplit('\\', 1)[0]).rsplit('\\', 1)[0]
> 
> 
> PyCharm inspection gave me warning on argument '\\' of the 2nd rsplit,
> 
> Expected type 'optional[bytes]' got 'str' instead
> 
> 
> If I changed it to,
> 
> rootPath = (rootPath.rsplit('\\', 1)[0]).rsplit(b'\\', 1)[0]
> 
> Python game me errors,
> 
> TypeError: Can't convert 'bytes' object to str implicitly
> 
> How to fix it?

Do you use Python 2 or 3? 

Is your version the one PyCharm expects and supports? If yes, consider 
filing a bug report (for PyCharm, not Python).

You should of course still follow Chris' advice and use os.path to 
manipulate file paths.




More information about the Python-list mailing list