Correct way to write line to file?

后端 未结 14 1922
日久生厌
日久生厌 2020-11-21 06:27

I\'m used to doing print >>f, \"hi there\"

However, it seems that print >> is getting deprecated. What is the recommended way t

14条回答
  •  不知归路
    2020-11-21 06:38

    Since 3.5 you can also use the pathlib for that purpose:

    Path.write_text(data, encoding=None, errors=None)

    Open the file pointed to in text mode, write data to it, and close the file:

    import pathlib
    
    pathlib.Path('textfile.txt').write_text('content')
    

提交回复
热议问题