TypeError: a bytes-like object is required, not 'str' when writing to a file in Python3

前端 未结 9 2157
Happy的楠姐
Happy的楠姐 2020-11-22 04:58

I\'ve very recently migrated to Py 3.5. This code was working properly in Python 2.7:

with open(fname, \'rb\') as f:
    lines = [x.strip() for x in f.readli         


        
9条回答
  •  野性不改
    2020-11-22 05:09

    why not try opening your file as text?

    with open(fname, 'rt') as f:
        lines = [x.strip() for x in f.readlines()]
    

    Additionally here is a link for python 3.x on the official page: https://docs.python.org/3/library/io.html And this is the open function: https://docs.python.org/3/library/functions.html#open

    If you are really trying to handle it as a binary then consider encoding your string.

提交回复
热议问题