Why truncate when we open a file in 'w' mode in python

前端 未结 8 2122
再見小時候
再見小時候 2020-12-13 05:40

I am going through Zed Shaw\'s Python Book. I am currently working on the opening and reading files chapters. I am wondering why we need to do a truncate, when we are alread

8条回答
  •  佛祖请我去吃肉
    2020-12-13 06:40

    With truncate(), you can declare how much of the file you want to remove, based on where you're currently at in the file. Without parameters, truncate() acts like w, whereas w always just wipes the whole file clean. So, these two methods can act identically, but they don't necessarily.

提交回复
热议问题