How to empty a file using Python

前端 未结 2 530
暖寄归人
暖寄归人 2020-12-01 11:53

In the Unix shell I can do this to empty a file:

cd /the/file/directory/
:> thefile.ext

How would I go about doing this in Python?

2条回答
  •  心在旅途
    2020-12-01 12:09

    Opening a file creates it and (unless append ('a') is set) overwrites it with emptyness, such as this:

    open(filename, 'w').close()
    

提交回复
热议问题