Python3 write gzip file - memoryview: a bytes-like object is required, not 'str'

后端 未结 4 1218
余生分开走
余生分开走 2020-12-16 13:33

I want to write a file. Based on the name of the file this may or may not be compressed with the gzip module. Here is my code:

import gzip
filen         


        
4条回答
  •  旧巷少年郎
    2020-12-16 14:14

    print is a relatively complex function. It writes str to a file but not the str that you pass, it writes the str that is the result of rendering the parameters.

    If you have bytes already, you can use fd.write(bytes) directly and take care of adding a newline if you need it.

    If you don't have bytes, make sure fd is opened to receive text.

提交回复
热议问题