Python file open/close every time vs keeping it open until the process is finished

后端 未结 4 1659
暗喜
暗喜 2020-12-24 11:29

I have about 50 GB of text file and I am checking the first few characters each line and writing those to other files specified for that beginning text.

For example.

4条回答
  •  离开以前
    2020-12-24 12:12

    Keep it open the whole time! Otherwise you tell the system that you are done writing all the time and it might decide to flush it onto the disk instead of buffering it. And for obvious reasons n disk writes are much more expensive than 1 disk write.

    If you want to append to the file and not overwrite it then yes, a is the correct mode.

提交回复
热议问题