How to Erase Contents of Text File in Python?

Problem Formulation and Solution Overview

In this article, you’ll learn how to erase the contents of a flat text file in Python.

  • Given the file name of a file with some content.
  • You want to erase all content of the file to obtain an empty one like this:

To make it more fun, we have the following running scenario:

A recent grad of UofT, Alise, has been granted an interview at AnnexIT. Their interview process involves solving coding questions. Alise can handle most of the questions but needs help with erasing the contents of a flat text file.

πŸ’¬ Question: How would we write Python code to erase the contents of a flat text file?

We can accomplish this task by one of the following options:


Contents of questions.txt

How would you find the Common Elements of Two Lists
How would you Sum Two DataFrame Columns
How would you Convert JSON to a DataFrame
How would you Download an Image from a URL
How would you Convert HEX to ASCII
How would you Erase the Contents of a Text File

Method 1: Use open() and close()

This example uses a one-liner to open() the above flat text file, erase the contents, and close() the same file.

open('questions.txt', 'w').close()

This code assumes the existing file is currently closed and opens the file in w (write) mode. Since no data is passed, the contents of the file are erased. This file saves and closes.

Output


Method 2: Use obj.open() and obj.close()

In this example, a file object is created when the existing flat text file is opened. Then, the file contents are erased, and the said object is referenced to close the file.

fp = open('questions.txt', 'w')
fp.close()

This code assumes the existing file is closed, opens the file in w (write) mode and assigns a file object, fp. Since no data is passed, the file contents are erased, saved, and the file is closed.

Output


Method 3: Use obj.open(), truncate() and obj.close()

In this example, a file object is created when the existing flat text file is opened. Then, truncate() is used to erase the contents and fp.close() is called to save and close the file.

fp = open('questions.txt', 'r+')
fp.truncate(0)
fp.close()

This code assumes the current file is open, re-opens the file in r+ (read) mode and assigns a file object, fp. Using r+ moves the file object to the beginning of the file. The method truncate() then erases the contents and fp.close() is called to save and close the file.

Output


Method 4: Use obj.open(), obj.seek() and truncate()

This example is similar to the above, except seek() is called to point to the beginning of the file. Then, truncate() is used to erase the contents and fp.close() is called to save and close the file

fp = open('questions.txt', 'a')
fp.seek(0)
fp.truncate()
fp.close() 

This code opens the above flat text file in a (append) mode and assigns a file object, fp. Append mode means the file is opened for writing.

If the file does not exist, one will be created.

The seek() method moves the pointer to the beginning of the file and truncate() erases the contents. The file object is then referenced to close the file.

Output


Summary

These four (4) methods to erase the contents of a flat text file should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!