今天用python写入文本,

1 file_object2 = open('result.txt', 'w')
2 file_object2.write(bookid_list)
3 file_object2.close( )

报错 TypeError: expected a string or other character buffer object

原因:

是写入文件要求写入内容是str,直接转换成str即可,如下:

1 file_object2 = open('result.txt', 'w')
2 file_object2.write(str(bookid_list))
3 file_object2.close()

补充:

还有一个读操作的代码:

1 file_object = open('thefile.txt')
2 try:
3      all_the_text = file_object.read( )
4 finally:
5      file_object.close( )

 

相关文章:

  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2021-08-28
  • 2021-10-16
  • 2022-12-23
猜你喜欢
  • 2022-03-04
  • 2022-12-23
  • 2021-10-17
  • 2022-01-28
  • 2022-01-24
  • 2022-12-23
相关资源
相似解决方案