Open file for reading and writing with truncate

后端 未结 2 912
孤城傲影
孤城傲影 2021-01-06 06:49

How do I open a file for reading and writing that is also truncated to 0?

I\'ve tried:

f = open(PATH, \'w+\')

I\'m unable to read f

2条回答
  •  离开以前
    2021-01-06 07:12

    If you want to store the data then truncate use r+:

    with open(PATH,"r+") as f:
        line = f.read()
        f.seek(0)
        f.truncate()
    

提交回复
热议问题