“for line in…” results in UnicodeDecodeError: 'utf-8' codec can't decode byte

前端 未结 10 856
抹茶落季
抹茶落季 2020-11-22 17:15

Here is my code,

for line in open(\'u.item\'):
#read each line

whenever I run this code it gives the following error:



        
10条回答
  •  野性不改
    2020-11-22 17:29

    Sometimes when open(filepath) in which filepath actually is not a file would get the same error, so firstly make sure the file you're trying to open exists:

    import os
    assert os.path.isfile(filepath)
    

    hope this will help.

提交回复
热议问题